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.

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示