wahidku Posted June 24, 2013 Share Posted June 24, 2013 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) Quote Link to comment https://forums.phpfreaks.com/topic/279497-correct-syntax-to-use-variable-inside-as-argument-of-_post/ Share on other sites More sharing options...
DaveyK Posted June 24, 2013 Share Posted June 24, 2013 You dont change it's name relatively, you change the value of the radio! Quote Link to comment https://forums.phpfreaks.com/topic/279497-correct-syntax-to-use-variable-inside-as-argument-of-_post/#findComment-1437614 Share on other sites More sharing options...
kicken Posted June 24, 2013 Share Posted June 24, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279497-correct-syntax-to-use-variable-inside-as-argument-of-_post/#findComment-1437621 Share on other sites More sharing options...
DaveyK Posted June 24, 2013 Share Posted June 24, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279497-correct-syntax-to-use-variable-inside-as-argument-of-_post/#findComment-1437624 Share on other sites More sharing options...
wahidku Posted June 24, 2013 Author Share Posted June 24, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279497-correct-syntax-to-use-variable-inside-as-argument-of-_post/#findComment-1437627 Share on other sites More sharing options...
Barand Posted June 24, 2013 Share Posted June 24, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279497-correct-syntax-to-use-variable-inside-as-argument-of-_post/#findComment-1437629 Share on other sites More sharing options...
DaveyK Posted June 24, 2013 Share Posted June 24, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279497-correct-syntax-to-use-variable-inside-as-argument-of-_post/#findComment-1437634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.