stijn0713 Posted August 2, 2012 Share Posted August 2, 2012 How can i make this string inside post ? if(isset($_POST["key($_SESSION['enquete_vragen'])"])){ } Quote Link to comment https://forums.phpfreaks.com/topic/266614-dynamic-value-in-_post/ Share on other sites More sharing options...
Pikachu2000 Posted August 2, 2012 Share Posted August 2, 2012 Huh? Quote Link to comment https://forums.phpfreaks.com/topic/266614-dynamic-value-in-_post/#findComment-1366439 Share on other sites More sharing options...
stijn0713 Posted August 2, 2012 Author Share Posted August 2, 2012 My submit button's name contains the id of my questions that i retrieve from the database (making a simple survey program). it's in a loop, so everytime the submit button has a different name (namely the id of the question). therefor i need this statement: $_POST('$question_id') but i don't know how to assign a variable in the string of the name of the post variable Quote Link to comment https://forums.phpfreaks.com/topic/266614-dynamic-value-in-_post/#findComment-1366444 Share on other sites More sharing options...
scootstah Posted August 2, 2012 Share Posted August 2, 2012 Something like this? $key = 'key' . $_SESSION['enquete_vragon']; if (isset($_POST[$key])) {} Note that you don't need to check if the specific submit button was set. You can simply check for a POST request, and then process it accordingly. You can do that with if (!empty($_POST)){} Quote Link to comment https://forums.phpfreaks.com/topic/266614-dynamic-value-in-_post/#findComment-1366445 Share on other sites More sharing options...
stijn0713 Posted August 2, 2012 Author Share Posted August 2, 2012 I think i was programming a bit too long, can't get rid of an error so i thought for i second the error came because i had to put quotes around the key($_SESSION['enquete_vragen'] to adress the name of the button which is obviously wrong. my mistake if i may, if(!empty($_POST[key($_SESSION['enquete_vragen'])])){ next($_SESSION['enquete_vragen']); stel_vraag(); } function stel_vraag (){ $vraag = current($_SESSION['enquete_vragen']); $id_vraag = key($_SESSION['enquete_vragen']); $getOptions = mysql_query("SELECT * FROM poll_options WHERE ID_question = '".mysql_real_escape_string($id_vraag)."' ORDER BY ID ASC") or die(mysql_error()); $row_options = mysql_fetch_assoc($getOptions); $content = '<form method="POST" action="">'; $content .= '<table>'; $content .= "<tr>$vraag</tr><hr />"; do { if($row_options['option_type'] == 1){ $content .= '<br /><tr><input type="radio" name="answer" id="'.$row_options['ID'].'" value="'.$row_options['ID'].'"/>'.stripslashes($row_options['poll_option']).'</tr>'; } else if ($row_options['option_type'] == 2) { $content .= '<br /><tr><input type="checkbox" name="answer" id="'.$row_options['ID'].'" value="'.$row_options['ID'].'"/>'.stripslashes($row_options['poll_option']).'</tr>'; } } while ($row_options = mysql_fetch_assoc($getOptions)); $content .= '<br /><br /><tr><input type="submit" name="'.$id_vraag.'" id="'.$id_vraag.'" value="volgende" /></tr>'; $content .= '</table></form>'; echo $content; } My post array Array ( [answer] => 36 [19] => volgende ) my session array Array ( [enquete_vragen] => Array ( [18] => Wat is je geslacht? [19] => Welke talen spreek je? [20] => Lees je veel boeken? [22] => Wat is jouw favoriete drink? ) [enquete_start] => start ) It correctly shows the first question with the options, and when pressed on the submit button, shows the second question with the options... but it won't continue after 2 questions Quote Link to comment https://forums.phpfreaks.com/topic/266614-dynamic-value-in-_post/#findComment-1366451 Share on other sites More sharing options...
scootstah Posted August 2, 2012 Share Posted August 2, 2012 Did you even read my post? Quote Link to comment https://forums.phpfreaks.com/topic/266614-dynamic-value-in-_post/#findComment-1366455 Share on other sites More sharing options...
stijn0713 Posted August 2, 2012 Author Share Posted August 2, 2012 Yes, ofcourse! I tried with if(!empty($_POST)) but it's the same. I think in this case if(!empty($_POST)) is equal result then if(!empty($_POST[key($_SESSION['enquete_vragen'])])) so the error is situated somewhere else. Sorry if i gave you the feeling that i rushed over your post Quote Link to comment https://forums.phpfreaks.com/topic/266614-dynamic-value-in-_post/#findComment-1366458 Share on other sites More sharing options...
scootstah Posted August 2, 2012 Share Posted August 2, 2012 No, those are not the same. if (!empty($_POST)) is checking if the $_POST superglobal is not-empty, which means there is a POST request. If you add a key in there, then you are only checking for that specific key. My point in all this is that you don't necessarily need to create a dynamic key from the session, you can simply accept any POST request, and then make sure the data is what you expected. In case you don't want to do that, I already showed you how to format the dynamic key. In case you missed it, here it is again: $key = 'key' . $_SESSION['enquete_vragon']; if (isset($_POST[$key])) {} Quote Link to comment https://forums.phpfreaks.com/topic/266614-dynamic-value-in-_post/#findComment-1366461 Share on other sites More sharing options...
stijn0713 Posted August 2, 2012 Author Share Posted August 2, 2012 I see the difference and i get your point of not necessarily needing to create a dynamic key from the session, but simply accepting any POST request, and then make sure the data is what you expected. However, the problem is not situated there and if it does, i'm completly not getting the point. Anyway, thanks already for the tip.! Quote Link to comment https://forums.phpfreaks.com/topic/266614-dynamic-value-in-_post/#findComment-1366465 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.