Jump to content

posting the value of an auto submit drop down list to another page


ajoo
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi all !

 

I wish to post the value of an auto submit dropdown value to another page, be redirected to that page, and  use it on that page. I am unable to achieve this in php and so I tried as :-follows:-

 

dropdown.php

<form method="post" action = dropdownaction.php>
    <select name="myselect" onchange="this.form.submit();">
        <option>blue</option>
        <option>red</option>
    </select>
</form>

and now I need the equivalent of 

 

dropdownaction.php

<?php

if(isset($_POST(['myselect']))) echo " I am selected".$_POST['myselect'];

?>

Please can someone tell me how I may retrieve the value of 'myselect' in the dropdownaction.php after being redirected to it.

Thanks loads.

 

Link to comment
Share on other sites

  • Solution

Your if statement should be

if(isset($_POST['myselect'])) echo " I am selected".$_POST['myselect'];

Also always enclose HTML attribute values in quotes too

<form method="post" action="dropdownaction.php">
...
Edited by Ch0cu3r
Link to comment
Share on other sites

Hi Ch0cu3r, 

 

Thanks for the reply. I'll remember to enclose the HTML attributes in quotes. Thanks.

 

However this does not work. ( How I wished it would ). But so far as I have read this does not work and we have to use javascript and we have to use the onChange event, like I have done in dropdown.php. The rest ( envoking the value of myselect) in and after being redirected to dropdownaction.php , however is still a mystery to me. 

 

Thanks !!

Link to comment
Share on other sites

Hi ! Ok so I have tried the code again as well. Just to be doubly sure that I have not made a mistake.  When I select the color Red from the drop down menu, I get this error message. 

 

( ! ) Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) in D:\xampp\htdocs\xampp\MagicOnn\testers\dropdownaction.php on line 3.

 

Thus I do not get message "I am selected" or the value of $_POST['myselect'].  Fastsol are you getting the message as well as the value of myselect?

Thanks all ! 

Link to comment
Share on other sites

Honestly I don't know what that error means.  Are you sure you changed the isset code like Ch0cu3r showed?  You had to many () in your original code.  This is what I have on my end that tested good.
 
select.php

<form method="post" action="dropdownaction.php">
    <select name="myselect" onchange="this.form.submit();">
        <option>blue</option>
        <option>red</option>
    </select>
</form>

dropdownaction.php

<!DOCTYPE html>
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
</head>

<body>
<?php
if(isset($_POST['myselect'])) echo " I am selected".$_POST['myselect'];
?>
</body>

</html>

Edited by fastsol
Link to comment
Share on other sites

@ajoo you get that error message because you have not change your if statement to the one I have posted!

 

This is because there is an error in your code. I have highlighted what is wrong below

if(isset($_POST(['myselect']))) echo " I am selected".$_POST['myselect'];
//             ^            ^
//             |            |
//            remove two braces, these are causing the error
                  
Edited by Ch0cu3r
Link to comment
Share on other sites

Yes Ch0cu3r, It has worked. I don't know why I got that message. I had removed the extra parenthesis. However I have noticed that it does not work for the first value of the list. Please suggest how that can be accomplished. 

 

fastsol thanks to you too. 

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.