开发者

PHP - Help with string delimiters in echoing a form

开发者 https://www.devze.com 2023-02-12 07:06 出处:网络
I am trying to build a HTML f开发者_运维问答orm if an item is in stock, but as I\'m storing the html in a string variable using single quotes to delimit it, and then setting form attributes using doub

I am trying to build a HTML f开发者_运维问答orm if an item is in stock, but as I'm storing the html in a string variable using single quotes to delimit it, and then setting form attributes using double quotes inside the form, how then can I refer to a php variable inside the form to dynamically assign a value to 'addItem'. If you look at my code you'll see what I mean! I will echo the contents of $summary after this has been done, and $summary at this point contains information extracted from my SQL database about the product.

$summary .= '<form id="testbasket" method="GET" target="_parent" action="cattest.php" >
        <input type="text" id="addItem" name="addItem" value='.'"<?php echo $plantname; ?>"'.'>
        <input type="submit" value="Add To Basket" />
        </form>';

Note: I'm using _parent as the target because this sits inside an iframe in the parent page.

Many thanks.


$summary .= '<form id="testbasket" method="GET" target="_parent" action="cattest.php" >
        <input type="text" id="addItem" name="addItem" value="' . $plantname . '" />
        <input type="submit" value="Add To Basket" />
        </form>';


Use concatenation with . like so:

$summary .= '<form id="testbasket" method="GET" target="_parent" action="cattest.php" >
        <input type="text" id="addItem" name="addItem" value="'.$plantname.'">
        <input type="submit" value="Add To Basket" />
        </form>';


<?php
  print("<div id = \"someID\">" . $someVariable . "</div>");
?>


You're on the right track, but the code you have outputs the code as a string. You can simply concatenate the $plantname variable into the string. Here is your code using correct quotes:

$summary .= '<form id="testbasket" method="GET" target="_parent" action="cattest.php" >
    <input type="text" id="addItem" name="addItem" value="'.$plantname.'">
    <input type="submit" value="Add To Basket" />
    </form>';

You also only need to use the php open and close tags <?php ?> when you have explicitly already left a php section. Since what you're writing is still within php, you simply use the variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号