Jump to content

Trouble with select / option tag and PHP


blogfisher

Recommended Posts

After wasting couple of hours on google.com for searching solution, i came here with hope to get quick reply. Well, i am developing an application in php. I am not sure how to access option tag value using php. My code snippet is as below:

 

echo '<select name="number" action="next.php" method="post">';
for ($i=1; $i <=10; $i++) {
   echo "<option value ='$i'>$i</option>";
}
echo '</select>

 

This basically shows you a drop-down list from 1 to 10. User can choose any one of them. As soon as user choose one of the option, i wan to pass the value to another file next.php. I dont want to use submit button. It should pass automatically.

 

I don't know how to do this.

 

Quick reply will be highly appreciated.

 

Thanks in advance.

Link to comment
Share on other sites

Hi,

 

I think you have to separate more clearly in your mind browser and server side.

 

What you need is a bit of browser scripting plus you need the form tags. If you use the select's onchange attribute:

 

<form name="nos" action="next.php" method="post">
  <select name="number" onchange="this.submit()">
<?php    
for ($i=1; $i <=10; $i++) {
   echo '<option value ="$i">$i</option>';
}
?>

  </select>

 

You might need to do onchange="document.forms('nos').submit()" but I think this will work. You will effectively be submitting the form if the select value is changed.

 

The fancy way to do this is with a bit of AJAX where instead of submitting the form you trigger an asyncranous request to the php without anything visible happening.

 

Martin

</form>

Link to comment
Share on other sites

The method posted will work, and in fact using javascript is the only way this can be done, but it's really bad for usability for a couple reasons. The first is that users who have javascript turned off will not be able to use this, and the second is that search engines don't use javascript, and as such won't be able to index content on the other side.

 

The best thing to do is to add the submit button, then hide it with javascript onload.

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.