简体   繁体   中英

Pass multiple values from javascript to php file

I'm trying to pass multiple values to the javascript function "get2" and then call "data2.php" file in order to retrieve data from the database according to the submitted values from the dropdown list/textbox. Both work separately but not together, the error I'm getting is "Undefined index". Can anyone help me please?

<script type="text/javascript">
    function get2() { 
        $.post('data2.php', { gender: form2.gender.value },
            function(output) {
                $('#gender').html(output).show();
            }
        );
        $.post('data2.php', { skills: form2.skills.value },
            function(output) {
                $('#skills').html(output).show();
        });
    }

</script>
...
...
<form name="form2">
        <Select name="gender">
            <OPTION>Male</OPTION>
            <OPTION>Female</OPTION></SELECT>

What is the patients relationship status?
(hold "Ctrl" key to select multiple options at one time):
<br/><br/>
        <BR>
        <BR>
        <select name="skills">
            <OPTION value="Single" selected="selected">Single</OPTION>
            <OPTION value="With partner">With partner</OPTION>
            <OPTION value="Separated from partner">Separated from partner</OPTION>
            <OPTION value="Partner died">Partner died</OPTION>
        <OPTION value="DK">DK</OPTION>
        </select>
        <BR>
        <BR>
        <INPUT TYPE="button" VALUE="search" onClick="get2();">
</form>
<div id="gender"></div>
<div id="skills"></div>

DATA2.PHP

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("grist");

$gender = $_POST['gender'];
$skills =  $_POST['skills'];

if (($gender==NULL) && ($skills==NULL)) {
    echo"please enter gender and skills!";
    }
else 
{
    $dob4 = mysql_query("SELECT * FROM patients WHERE gender='$gender' AND relationship_status='$skills'");
    //$dob_num_rows = mysql_num_rows($dob);
    while($row4 = mysql_fetch_array($dob4)){
    $a=$row4['patient_id'];
    $b=$row4['gender'];
    $c=$row4['dob'];
        echo "<b>Patient:</b> $a";
        echo "<b>Patient:</b> $b";
        echo "<b>Patient:</b> $c";
    }
}
?>

This is how you can send two values to a page using a POST request:

<script type="text/javascript">
    function get2() { 
        $.post('data2.php', { gender: form2.gender.value, 
                              skills: form2.skills.value },
            function(output) {
                $('#gender').html(output).show();
                $('#skills').html(output).show();
            }
        );
    }
</script>

Note that now this updates both the gender and skills elements. I'm not sure what you want to do; data2.php requires/permits using both parameters.

You should also sanitize the user input: currently you are vulnerable to SQL injection attacks. It would be even better to use a database client library that permits using prepared statements, such as PDO. The old mysql library you are using is deprected and will be removed from PHP at some point.

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