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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.