Jump to content

Correct syntax to use variable inside as argument of $_POST[ ]


wahidku

Recommended Posts

I have a input tag

   

   <input type="radio" name="<?php echo $serial_number; ?>" />

 

now i want to save the value into a variable like

  

    if(isset($_POST['next']))
        {
            $answer = $_POST['$serial_number'];  // here is wrong

 

what is correct syntax. can anybody help me.

 

N.b: $serial_number is like (1,2,3..etc)

  On 6/24/2013 at 11:18 AM, kicken said:

To answer your actual question however, the proper syntax would be:

$_POST[$serial_number]
You do not put quotes around the variable, you only do that for strings used as keys.

 

 

or do:

$_POST["{$serial_number}"]

also works.

The correct way is

<input type="radio" name="serial_number" value="<?php echo $serial_number ?>" />

If the radio has a variable name you have no idea which POST variable to process - it could be called anything.

  On 6/24/2013 at 11:52 AM, wahidku said:

Thanks DaveyK for your reply.

I try both of that syntax.

 

    $_POST[$serial_number]

 

and

    $_POST["{$serial_number}"]

 

but both causes an error Undefined index. 

 

In that case you need to check where the var $serial_number is set. It appears to not exist.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.