weixin_33676492 2018-07-05 11:34 采纳率: 0%
浏览 9

PHP中的Ajax发布方法

I am trying to send ajax post javascript variable to php. My code php will only be executed if I press submit in another form. My code is in one index.php file.

The console shows that this value has been sent, but my php code does not want to pick it up and does not execute the query. Why?

<?php
if (isset($_POST['imie2'])) {

...

if(isset($_POST['item_id']) && !empty($_POST['item_id'])){
$value = $_POST['item_id'];

if ($polaczenie->query("INSERT INTO zamowienia VALUES ('$value')")) {
...

?>


<form method="post">
<input type="text" name="imie2">
...
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script>
    $(document).ready(function () {
        var value = localStorage.getItem('sumalist');
        console.log(value);

        $.ajax({
            url:"index.php",
            method:"POST",

            data:{
                item_id: value,
            },
            success:function(response) {
                console.log('ok'); // console shows ok
            },
            error:function(){
                alert("error");
            }

        });
    });

</script>
  • 写回答

1条回答 默认 最新

  • weixin_33716154 2018-07-05 11:43
    关注

    You said:

    if (isset($_POST['imie2'])) {
    

    but your data looks like this:

    data:{
       item_id: value,
    },
    

    There's no imie2 field so your test will always fail.

    评论

报告相同问题?