简体   繁体   中英

How to send multiple variable data to ajax and receive with PHP?

I'm trying to send variables data using ajax but I don't know what will be the property of data: in $.ajax I don't know if I'm using it right and heres how I declared my variable:

var value1 = 'value1';
var value2 = 'value2';
var value3 = 'value3';

How do I apply it on the ajax function?

$.ajax({
    url: "insert.php",
    method: "post",
    data:{value1:value1, value2:value2, value2:value2}, 
    dataType:"text",
    success:function(data){
        alert('Successfully')
    }
});

And here is how I try fetching this data from my insert.php :

$value1 = mysqli_real_escape_string($con,$_POST['value1']);
$value2 = mysqli_real_escape_string($con,$_POST['value2']);
$value3 = mysqli_real_escape_string($con,$_POST['value3']);

you create datastring like this

var dataString = 'value1='+ value1+ '&value2='+ value2;

    $.ajax({
                url: "insert.php",
                method: "POST",
                data:dataString , 
                dataType:"text",
                success:function(data){
                    alert('Successfully')
                 }
             });

your code have problem in this line

data:{value1:value1, value2:value2, value2:value2}, 

you send value2 two time so update this

data: {value1: 'value1', value2: 'value2', value3: 'value3'},

check this code . it will work and you can test this also

this is html code

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {

            $("#category").change(function () {
                var value1 = 'value1';
                $.ajax({
                    type: 'post',
                    url: 'edit.php',
                    data: {value1: 'value1', value2: 'value2', value3: 'value3'},
                    datatype: 'json',
                    success: function (data) {
                        alert(data);
                    }

                })
            })
        })
    </script>
    <title></title>
</head>
<body>
<form method="post">
    <table>
        <tr>
            <td>category:</td>
            <td>
                <select id="category">
                    <option>Select Category</option>
                    <option value="mobile">Mobile</option>
                    <option value="TV">Tv</option>
                    <option value="Phone">Phone</option>

                </select>
            </td>
        </tr>

    </table>
</form>

</body>
</html>

then this is insert.php. i write this code for your test purpose

<?php
print_r($_POST);
echo $value1 = $_POST['value1'];
echo "<br>";
echo $value2 = $_POST['value2'];
echo "<br>";
echo $value3 = $_POST['value3'];

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