dousuo2812 2013-02-15 14:47
浏览 27
已采纳

jquery post值到php

I am new to jquery also to php so my question sound quite simple or silly. Any help would be appreciated. I have an html in where i add and delete rows via buttons. And I want to update database by these data by another button. So i need the get length of my table to insert them within a loop in php side. How can i tranfer data to php side? I tried something like that.. As javascript code

function Length()
       {

            var table=document.getElementById("Table");
            var len = table.rows.length;
            $.post('/myproject/update_data.php', { "CONTROL": "len" } );
       }

on html;

<input type="submit" name="UPDATE" id="UPDATE" value="UPDATE" onclick="return Length();"/>

on php side it goes like

<?php
if(isset($_POST["UPDATE"]))
               {    
                    $length=$_REQUEST["CONTROL"];
               }
?>

And i get CONTROL is not defined. Is it a syntax wrong that i type directory of php file? Or any thing else? I couldnt get. Thank you

  • 写回答

4条回答 默认 最新

  • duanguangsong2380 2013-02-15 14:53
    关注

    You can't mix an AJAX post with a form submission, those are two separate requests. The form is submitting before the AJAX request is completed, so the form submit is the request your server receives.

    I'd recommend switching entirely to an AJAX request and add return false; to the end of the function, which will cancel the form submit and allow the AJAX request to do the work itself. Like this:

    $.post('/myproject/update_data.php', { "CONTROL": len, "UPDATE": "UPDATE" } );
    

    Alternatively, you can skip the AJAX and use JS to add a hidden input to your form.

    Add this to your HTML:

    <input type="hidden" name="CONTROL" val="">
    

    Add this to your JS:

    $('[name="control"]').val(len)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False