0

I am having some trouble with an onchange event. The task is a large section of code for sorting data but to try through isolating code ive tracked it down to the onchange event not actually working and I cant see whats wrong.

<?php   include("db_connect.php"); 

$query = 'SELECT * FROM db_class_catagories';
$result = mysqli_query($dbconnection,$query);
?>


<select id="primary_catagory" name="primary_catagory" onchange = "test_function(this)"style="position:absolute;left:10px;top:10px;width:315px;height:20px;z-index:11;text-align:left;">
<option value=''>Select</option>
<?php while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
    echo "<option value='" . $row['primary_catagory'] . "'>" . $row['primary_catagory'] . "       
</option>";}
?>
</select>

<?php
function test_function($primary_catagory)   {
echo $primary_catagory;
}
?>

This list is being generated from a database and is being called perfectly well, but no matter what I select nothing will happen. The main script used AJAX but again nothing was occuring and all I could think of is the onchange is not sending data.

I was using this example but couldnt get it working. How can i populate a dropdown list by selecting the value from another dropdown list

My task is more or less the same but something is not right with my onchange, any idea what daft thing im missing? thanks

2 Answers 2

2

The onchange event is a browser event, so test_function() must be a JavaScript function. It could use Ajax to send information about the selection to a server-side PHP listener, but the onchange event must be handled at the client.

There is a simple example here: Why am I not getting the value from onChange with Select? Just put your Ajax call where the alert is (in the answer, not the question.)

Sign up to request clarification or add additional context in comments.

Comments

1

Well, test_function is in PHP on the server, and onchange is on the client/browser. They cannot talk directly to each other.

Try adding some Javascript (and jquery) to the html page.

 <script>
 function test_function(e) {
     $.getJSON("server.php", {
         "action": "save",
         "val": $(e.currentTarget).val()
     }
 }
 </script>

Now the inching event will execute this javascript function, which will load that server.php page in the background.

(this is untested code)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.