Jump to content

select list selected value


Recommended Posts

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 by garevn
  • Like 1
Link to comment
Share on other sites

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 by dsonesuk
Link to comment
Share on other sites

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,



Edited by jxfish2
Link to comment
Share on other sites



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:



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






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.



Link to comment
Share on other sites

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 by JamesB
Link to comment
Share on other sites

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 by jxfish2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...