Shadowing Posted March 11, 2012 Share Posted March 11, 2012 Hey guys I wasnt sure if this is a php topic or java script im trying to make my drop down menu's activate onchange. what confuses me is how do I make php know that the java script is being run. Or have the java script trip my php code. To know a button has been pressed i use "if (isset['_POST'])" echo '<select name="siege_list" id="siege_list">'; foreach($name as $key => $value) { echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $current1 ['name'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options>'; } echo '</select><input type="submit" id="siege_planet" onchange="this.form.submit()">'; Quote Link to comment Share on other sites More sharing options...
redsmurph Posted March 11, 2012 Share Posted March 11, 2012 All PHP code has already run when Javascript scripts are performed, so you either have to make something happen on the page via Javascript (e.g. change the page appearance etc via DOM editing), or send data to a PHP script (and indirectly the database) using Ajax. What should happen on onchange? Quote Link to comment Share on other sites More sharing options...
Shadowing Posted March 11, 2012 Author Share Posted March 11, 2012 thanks for the reply redsmurph in this case it changes the planet a person is viewing. queries some information and sets it as a Session so when selection changes $planets3 = "SELECT address FROM planets WHERE name ='".mysql_real_escape_string($_POST['siege_list'])."'"; $planets2 = mysql_query($planets3) or die(mysql_error()); $planets1 = mysql_fetch_array($planets2); $_SESSION['planet'] = $planet1['address']; so i dont know how to make java script fire the php code Quote Link to comment Share on other sites More sharing options...
redsmurph Posted March 11, 2012 Share Posted March 11, 2012 You can't "fire" the PHP other than by making an HTTP request from Javascript or perform submit on a form. In your case I would suggest this: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml PHP is used to perform the server-side tasks and content creation. Javascript is part of the content creation, hence is performed after PHP has done its bits. Quote Link to comment Share on other sites More sharing options...
cpd Posted March 11, 2012 Share Posted March 11, 2012 If your submitting a form the submit button will submit it automatically provided the action specified in the form tag is set to the nothing or the page itself with additionaly $_GET variables. E.g. <form method="post"> <input type="submit" name="subForm" value="Submit" /> </form> If you wanted to reload the page on the change of a dropdown you can add the following to your select tag. <form id="theForm" method="post"> <select name="someName" onchange="javascript: document.forms["theForm"].submit();"></select> </form> Quote Link to comment Share on other sites More sharing options...
redsmurph Posted March 11, 2012 Share Posted March 11, 2012 This is how I do it, usually. I don't know if the longer syntax would be preferable. <select name='whatever' onchange='this.form.submit()'> Quote Link to comment Share on other sites More sharing options...
cpd Posted March 11, 2012 Share Posted March 11, 2012 This is how I do it, usually. I don't know if the longer syntax would be preferable. <select name='whatever' onchange='this.form.submit()'> I'm not convinced this would work because by saying "this" your addressing the select tag. You then try to access a "form" attribute of the select tag which is non-existent. Perhaps I'm wrong though? Have you tested it? Quote Link to comment Share on other sites More sharing options...
Drummin Posted March 11, 2012 Share Posted March 11, 2012 Ya, the onchange="this.form.submit()" needs to be in your select line. And within form tags, so as a stand-alone it would be echo '<form action="" method="post">'; echo '<select name="siege_list" id="siege_list" onchange="this.form.submit()">'; foreach($name as $key => $value) { echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $current1 ['name'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options>'; } echo '</select>'; echo '</form>'; Quote Link to comment Share on other sites More sharing options...
redsmurph Posted March 11, 2012 Share Posted March 11, 2012 I'm not convinced this would work because by saying "this" your addressing the select tag. You then try to access a "form" attribute of the select tag which is non-existent. Perhaps I'm wrong though? Have you tested it? Yes, I always use this within forms. Quote Link to comment Share on other sites More sharing options...
cpd Posted March 11, 2012 Share Posted March 11, 2012 Never realised that would work. Need to read-up on it I think. Quote Link to comment Share on other sites More sharing options...
Shadowing Posted March 11, 2012 Author Share Posted March 11, 2012 my query is running off of a SESSION that gets changed according to what is selected. So I guess the only way i can make this work is if the page reads the planet from the link instead of using Session then? that makes sense since java script is read after php. I have some thinking to do then, not sure i want to use $_GET for this. Quote Link to comment Share on other sites More sharing options...
redsmurph Posted March 11, 2012 Share Posted March 11, 2012 POST is typically preferred: Not visible on the address bar, more data can be transferred, and required if you transfer files (via an input type=file field). There might be cases where document.forms["theForm"].submit(); is preferred too. I'll check that. Clearly it's needed if you want to address a certain form rather than the one the field is in. 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.