简体   繁体   中英

how to run javascript code and PHP code when a variable selected from drop list

I have this drop down list that when I click on it, it shows me a hidden div

<form action="" method="post" name="select_name_form">
<input type="submit" name="select_name_submit" value="debts"/>
<select onchange="showMe(this);" name="select_name">
<?php foreach($result1 as $name) { ?>
<option value="<?php echo $name['name'] ?>"><?php echo $name['name'] ?></option>
<?php } ?>
</select>

</form>

And this is the javascript code:

<script type="text/javascript">
function showMe(e) {
    var strdisplay = e.options[e.selectedIndex].value;
    var e = document.getElementById("section3");
    if(strdisplay == "Hide") {
        e.style.display = "none";
    } else {
        e.style.display = "block";
    }
}
</script>

What I want to make when I click on any value of this list is still shows me the hidden div but I want to apply this PHP code and select data into that div:

if(isset($_POST['select_name_submit'])){
    $name_selected = $_POST['select_name'];
    try{
        $query = ("SELECT * FROM debts WHERE name=:name");
        $stmt = $conn->prepare($query);
        $stmt->bindValue(":name", $name_selected);
        $count = $stmt->execute();
        //header("location: debts.php");
    }
    catch(PDOException $e) {
        echo $e->getMessage();
        header("location: debts.php");
    }

}

This PHP code is in the same page where I have the drop down list. So how can I make the drop list run 2 code when a name is selected from it (one code is JS and the second is PHP).

The scenario which you mentioned can be achieved using ajax,

http://www.w3schools.com/php/php_ajax_database.asp

Please refer this example, you will get more idea.

in case needed, please add comment, i can help you.

Thanks Amit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM