0

Im trying to assign the value of a php variable to a hidden element. But cant get it to work

Here's what Ive tried so far

Attempt 1:

echo '<input type="hidden" maxlength="100" name="value" id="val" value= "${myOb->content}"/>'; 

Attempt 2:

echo '<input type="hidden" maxlength="100" name="value" id="val" value= "<?php echo $myOb->content"/>'; 

On both counts . I just get the hard coded value ie. ${myOb->content} or <?php echo $myOb->content in the html element rather than the server side variable. Any thoughts on what Im doing wrong?

3
  • PHP code cannot be nested within itself. echo "<?php echo 'foo' ?>" is going to print PHP code, not "foo". and even then, you're using the wrong type of quotes to allow embedding variables in text anyways. Commented Nov 27, 2013 at 20:59
  • @MarcB : Im relatively new to PHP . Any thoughts on what I should do, to get my expected result? Commented Nov 27, 2013 at 21:04
  • 1
    just like FreshPrince has below, minus the syntax error in the variable. it should be just $myobj->whatever. the {} brackets should only be used on variables within strings, e.g. echo "{$foo}" Commented Nov 27, 2013 at 21:05

3 Answers 3

1

try

    <?php echo '<input type="hidden" maxlength=100 name="value" id="val" value="'.$myobj->foo.'">';

This works for me. Good luck

Sign up to request clarification or add additional context in comments.

Comments

0

When you use single quotes with echo, you're asking for the literal string value. You can try:

echo '<input type="hidden" maxlength="100" name="value" id="val" value= "' . ${myOb->content} . '"/>'; 

2 Comments

I get Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) on that line. Any idea why that could be happening?
Try with $myObj->content
0

If anyone need this in the future "'.strval($recordId).'" this worked for me.

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.