1

I want to call php function in dropdown menu by onchange event. I want with choose one of the options, the appropriate valuse are read from database and are list in another dropdown menu.

code:

<?php

function read() {

mysql_connect("localhost", "username", "password");

mysql_select_db("database_name");

$sql = mysql_query("SELECT name FROM table");

if (mysql_num_rows($sql)) {
    $select = '<select name="select">';
    while ($rs = mysql_fetch_array($sql)) {
        $select.='<option value="' . '">' . $rs['name'] . '</option>';
    }
}
$select.='</select>';
echo $select;
} 
?>

    <!--html code -->

    <select onchange="document.write('<?php read(); ?>');">
    <option value="0">a</option>
    <option value="1">b</option>
    <option value="2">c</option>
    </select>

This code output: enter image description here

My desired output: enter image description here

How can I get My desired output ? Thanks

4
  • 2
    You use AJAX to call a script and get the data back. Commented Jun 9, 2016 at 16:28
  • 1
    You have to interact with the dropdown using Javascript. PHP does all its work before the page is shown and will not work on a rendered page without the help of a front-end scripting language, such as Javascript. Commented Jun 9, 2016 at 16:33
  • PHP runs on the server, and cannot be "called" by JS code. you can do an ajax request to invoke a php script, but that's not "calling" a php function. Commented Jun 9, 2016 at 16:39
  • Do you need a value from the (a,b,c) select list in order to display the new select list? I don't see that in your example. Commented Jun 9, 2016 at 16:46

2 Answers 2

1

Just to explain:

PHP code is executed before the page is rendered in your user's browser (Server side).

In the other hand, Javascript is executed in the Client-side. It means that php finnished execution already.

If you wanna call a php function, you will have to make another request to the Server.

To do that "on the fly", you will have to use AJAX, as meantioned by @Jon in the comments.

Here is an example using jQuery (Just a javascript library, to simplify our task):

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javasript">

    //Listen to select 'onchange' event
    $('select#your_select_id').change(function(){

        //Read selected value
        var inputValue = $(this).val();

        //Make an ajax call
        $.post('ajaxscript.php', { value: inputValue }, function(data){

            //The return of 'read' function will be accessible trough 'data'

            //You may create DOM elements here
            alert('Finnished');

        });

    });

</script>

and here is our ajaxscript.php content:

<?php
//Declare (or include) our function here
//function read(){ ... }

$value = $_POST['value']; //Selected option
//...

echo read();
Sign up to request clarification or add additional context in comments.

Comments

1

Hi You can also use javascript form submit in this and call a php function

<?php
function read() {
mysql_connect("localhost", "username", "password");
mysql_select_db("database_name");
 $sql = mysql_query("SELECT name FROM table");
if (mysql_num_rows($sql)) {
$select = '<select name="select">';
while ($rs = mysql_fetch_array($sql)) {
$select.='<option value="' . '">' . $rs['name'] . '</option>';
 }
}
 $select.='</select>';
echo $select;
 } 
 if (isset($_POST['value'])) {
  read($_POST['value']);
}
    ?>
  <form method="POST">
 <select name="value" onchange="this.form.submit()">
 <option value="0">a</option>
<option value="1">b</option>
<option value="2">c</option>
</select>
 </form>

1 Comment

I test this and worked but Is it logically true (send form) ?

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.