Jump to content

using onchange for a drop down menu to activate php


Shadowing

Recommended Posts

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()">'; 

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.