how to write a php code in a php variable?

Discussion in 'PHP' started by JEET, Jul 9, 2006.

  1. #1
    Hi,
    I am creating a form which should have the following code:
    <input type='text' value='<? echo $var; ?>'>

    This form has to be created using a php program.
    The problem is that I cannot store this string above in a php variable.
    $p = "<input type='text' value='<? echo $var; ?>'>";
    Anyone here knows a simple method of doing this?
    thank you
     
    JEET, Jul 9, 2006 IP
  2. CCD

    CCD Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Assuming you have $var stored, then $p can be set how you want as follows:

    $p := '<input type="text" value="'.$var.'">';

    Choice of quotes is reversible but important to avoid having to 'escape' characters.
    Hope this helps, if not post why and I'll take a closer look :)
     
    CCD, Jul 9, 2006 IP
  3. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm curious, and maybe I missed it somewhere along the way, but what are the drawbacks to escaping characters vs using different quotes like you have there?
     
    ip076, Jul 9, 2006 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    No, I cannot do this.
    I don't have to write a value in the form code while creating it. Basically the script which creates the form is called only once. Then form code is written in a file.

    But the form is called over and over from another script"s". Each time the value of $var could be different based on what user selected in that script which he is using now.
    It is the PHP code which has to be written in that file embedded within the form code.

    The bigger problem is that this is not the only field which needs php code embedded.
    Thank you for the help.
     
    JEET, Jul 10, 2006 IP
  5. CCD

    CCD Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ip076 - nodrawbacks to escaping, just I find it easier to avoid when I can... lots of slashes or repeated characters can become a nightmare to debug.

    Jeet - if I understand you correctly, how about this:

    echo '<input type="text" value="'.$var.'">';
     
    CCD, Jul 10, 2006 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    Hi CCD,
    Thanks for the help but this is not what I need.
    I figured out a way of doing this and if it works, I will post here. I think it will work...
    Will let you know.
    Bye
     
    JEET, Jul 10, 2006 IP
  7. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #7
    I am not sure... but try the following

    $p = "<input type='text' value='&lt;?=$var; ?>'>";
     
    PinoyIto, Jul 10, 2006 IP
  8. dddougal

    dddougal Well-Known Member

    Messages:
    676
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    108
    #8
    $p = "<input type=\'text\' value=\'$var\'>";

    will work fine
     
    dddougal, Jul 11, 2006 IP
  9. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for the info! I'll agree with you there that it can sometimes be a pain to debug.
     
    ip076, Jul 11, 2006 IP
  10. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #10
    
    	function createInputField($value){
    		return "<input type='text' value='". addslashes($value) . "'>";
    	}
    
    PHP:
    Bobby
     
    Chemo, Jul 11, 2006 IP