2
<?php
if(isset($_POST['btnLogin'])){
    $myVariable = $_POST['fieldParameter'];

    if(condition){
        //do something
    }else{

      echo "  
        <form method='POST' action='submit.php'><br/>       
          <input type='hidden' name='myVariable' value='<?php echo $myVariable; ?>'/>
          <br/>
          <input type='submit' name='btnSubmit' id='submit' value='Submit'>
        </form>

          ";
        }
}
?>

Notice that the variable $myVariable is contained in the main IF block. I'm trying to send the value of $myVariable to submit.php as hidden field.

Also, i enclosed all the html tags using one echo statement with double quotes.

I found related questions here in SO but can't find similar to embedding php within a long echo of html tags

I tried to put value='<?php echo $studentNo; ?>' with no success.

I want to access it in a submit.php file like this,

submit.php

<?php 
    $aVariable = $_POST['myVariable'];
    echo $aVariable;
?>

How can I pass the value contained in $myVariable as hidden field? Is there something wrong with the way I use double and single quotes?

5
  • 2
    You just need to type $myVariable instead of <?php echo $myVariable ?> in your string. Double quotes "" only creates a string literal. It doesn't directly output data like inline HTML. As you can see from the syntax coloring in StackOverflow, the <?php inside a string is sent as-is. You can also see this via view-source in your browser. Commented Nov 5, 2016 at 15:13
  • Thank you. I just tried this <input type='hidden' name='studentNo' value='$studentNo'/> and it worked. Commented Nov 5, 2016 at 15:14
  • looks like it worked with single quotes while being contained in one long echo " //html here <input type='hidden' name='studentNo' value='$studentNo'/> "; Commented Nov 5, 2016 at 15:16
  • I am referring to the double quotes that embed your whole HTML output. Commented Nov 5, 2016 at 15:16
  • @PEMapModder Sure. I appreciate the help. :) Need to study more about Php. Things are a bit different compared to Java. I guess I'm looking for access modifiers and class attributes. Commented Nov 5, 2016 at 15:18

2 Answers 2

4

If you are already echoing a string you shouldn't put <?php echo "" ?> inside it again. You should concatenate your string instead. But in your case you don't even need to do that, because you're using double quotes for echoing which means you can simply just write your variable in it.

echo "<form method='POST' action='submit.php'><br/>       
      <input type='hidden' name='myVariable' value='$myVariable;'/>
      <br/>
      <input type='submit' name='btnSubmit' id='submit' value='Submit'>
    </form>";

If you were using single quotes for your echo, it would look like this:

echo '<form method="POST" action="submit.php"><br/>
    <input type="hidden" name="myVariable" value="' . $myVariable . '"/><br/>
    <input type="submit" name="btnSubmit" id="submit" value="Submit">
</form>';
Sign up to request clarification or add additional context in comments.

Comments

0

You just need to type $myVariable instead of in your string. Double quotes "" only creates a string literal. It doesn't directly output data like inline HTML. As you can see from the syntax coloring in StackOverflow, the

You can try these variants (simplified):

// code before
echo "<input type='hidden' name='myVariable' value='$myVariable'/>";
// code after

// OR //

// code before
?>
<input type='hidden' name='myVariable' value='<?= $myVariable ?>'/>
<?php
// code after

Note that the quotes you use in HTML don't affect PHP, as long as you escape them properly (use \" and \' where appropriate).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.