blogfisher Posted December 7, 2008 Share Posted December 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
msinternet Posted December 7, 2008 Share Posted December 7, 2008 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> Quote Link to comment Share on other sites More sharing options...
blogfisher Posted December 7, 2008 Author Share Posted December 7, 2008 Thankx martin for guideline. It works with little change. Instead of onchange="this.submit()", it needs to be onchange="form.submit()". This works, Thanks a lot for your quick reply. Quote Link to comment Share on other sites More sharing options...
haku Posted December 8, 2008 Share Posted December 8, 2008 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. Quote Link to comment Share on other sites More sharing options...
blogfisher Posted December 8, 2008 Author Share Posted December 8, 2008 Haku. Can you please explain in details with code snippet ? "The best thing to do is to add the submit button, then hide it with javascript onload."- how ? Quote Link to comment Share on other sites More sharing options...
haku Posted December 9, 2008 Share Posted December 9, 2008 <input type="submit" id="submit_button" value="submit" /> window.onload=function() { document.getElementById('submit_button').style.display='none' } Note: Not tested for typing errors. Quote Link to comment 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.