garevn Posted August 21, 2014 Share Posted August 21, 2014 (edited) I submit this select list through a form, If the chosen number exist then I print an error but the selected value resets. Can I somehow make it remember the selected value? var myDate = new Date();var year = myDate.getFullYear();for(var i = 4; i < 16; i++){document.write('<option value="'+i+'">'+i+'</option>');} When I use Input I use this and it works fine but now I am a bit confused. <?php if (isset($_POST['name'])) echo 'value="'.$_POST['name'].'"';?> Edited August 21, 2014 by garevn 1 Link to comment Share on other sites More sharing options...
dsonesuk Posted August 21, 2014 Share Posted August 21, 2014 (edited) You shouldn't use document.write. You should create options using add() http://www.w3schools.com/jsref/met_select_add.asp. When you select an option and submit, the page reloads and resets the select listing, unless you use php to identify what was selected and add attribute selected="selected" to that specific option. Edited August 21, 2014 by dsonesuk Link to comment Share on other sites More sharing options...
jxfish2 Posted August 25, 2014 Share Posted August 25, 2014 (edited) I have a number of PHP / HTML pages that are database driven. I can query the database, and update the database, using various forms that are tied to an external page. I am at a point now, where each listed data element, or asset, has a priority associated with it. If possible, I would like to display the priority column with values between 1 and 5. However, I need to allow our team members, those who have authority to make changes, the ability to change the priority of any given asset, as easily, and expeditiously as possible. I know that I can display the priority values in a dropdown format, with the current priority set to display for each asset. My question is, how can I display these default values, allowing the end user to reset the priority, by simply selecting a different drop-down priority value? I know that I can do this using a separate "Submit" button, but the page is already too crowded. I would really prefer to submit the data, as soon as the drop-down selection is made, without the need to press, or select another action button. Is this possible, and if so, how? Thanks in advance, JCF Edited August 25, 2014 by jxfish2 Link to comment Share on other sites More sharing options...
JamesB Posted August 25, 2014 Share Posted August 25, 2014 <select onchange="submitTheForm()"><select><form id='theForm'></form> function submitTheForm(){var form = document.getElementById('theForm');form.submit();} Link to comment Share on other sites More sharing options...
jxfish2 Posted August 25, 2014 Share Posted August 25, 2014 JamesB, Thanks for your post. I am trying to use a syntax that is close to what you posted, because I am more familiar with a syntax somewhat different from what you posted. Using the below syntax, I am getting the dropdown boxes to appear correctly, but I am unsure of how to make this change to the database. Here is what I have so far, and again, at least the values are showing properly in the dropdown box: <td> <form id-'changePriority'> <input type="hidden" name="alt" value="<?php echo $alt;?>" /> <input type="hidden" name-"loc" value="<?php echo $loc;?>" /> <center><select> onchange="ChangePriority()" name="Priority"> <option value="<?php echo $row['priority'];?>"><?php echo $row['priority'];?></option> <option value="1">1</option> <option value="2">2</option> ................. </center> </form> </td> <?php> function ChangePriority() { mysqli_query($con, "UPDATE table SET priority='$Priority' WHERE alt='$alt' and loc='$loc'"); } It appears as if the form is working, but the function is not. Any assistance would be greatly appreciated. JCF Link to comment Share on other sites More sharing options...
JamesB Posted August 25, 2014 Share Posted August 25, 2014 (edited) You can't call a PHP function from JavaScript code directly. html and javascript runs on the client-side. (in web browsers) php runs on the server-side. If you want client-side code to call server-side code, eg. the javascript code onchange="ChangePriority()" to call your PHP ChangePriority() function, then communication needs to happen between the client and the server. One way for them to communicate on the web is by submitting a form. So you can make the JavaScript code submit the form when the select changes, like in my previous post, then in your PHP page you can check if the form was submitted, and fetch the value with $_POST['Priority'], then you can call the PHP function ChangePriority(). Edited August 25, 2014 by JamesB Link to comment Share on other sites More sharing options...
dsonesuk Posted August 25, 2014 Share Posted August 25, 2014 err shouldn't this be under your own topic, instead of hijacking someone else's. Link to comment Share on other sites More sharing options...
jxfish2 Posted August 25, 2014 Share Posted August 25, 2014 (edited) Thanks James, You answered one of my questions. I built a huge set of data driven, web-based tools, all with PHP and HTML. These pages are working very well, and I've gotten a lot of "atta-boys". The company (a fortune 500 company) has already told me that I can patent these tools within their infrastructure. But again, I have managed to complete the entire project with nothing but PHP and HTML so far. To keep it simple, I would prefer to stay with PHP and HTML if possible. I know one way to do it using only these two languages, but it is probably not the best way, and I was hoping to find a simpler, more efficient way to accomplish my end results. I've become very efficient with what I do know, but I'm self-taught, and there is a lot that I have not done, or tried before. Edited August 25, 2014 by jxfish2 Link to comment Share on other sites More sharing options...
jxfish2 Posted August 25, 2014 Share Posted August 25, 2014 Sorry, I put the post here, because it was similar to the previous post. I did not realize that I needed to create a new post, for a similar topic. Link to comment Share on other sites More sharing options...
JamesB Posted August 25, 2014 Share Posted August 25, 2014 If you want to submit a form when a <select> changes, i'm almost certain you need to use JavaScript. Link to comment Share on other sites More sharing options...
jxfish2 Posted August 25, 2014 Share Posted August 25, 2014 Thanks for your help James, Have a great week. JCF Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now