Jump to content

dynamic value in $_POST


stijn0713

Recommended Posts

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

Link to comment
Share on other sites

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)){}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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])) {}

Link to comment
Share on other sites

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.!

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.