dsa5233 2013-06-17 20:55
浏览 21
已采纳

如何使用jquery发布到新的php页面

I am trying to post the element information that jQuery pulls, when a user clicks on table cell, to a new page that will use that information (an id in this case) in a sql query. i.e., the user clicks a cell and the job he/she clicks has an id of 25, that is to be passed to my php page that queries the database for the job with that id and then populates the page with said information. The user can then alter the information from the query and submit it to update the database table. I have the id from the click function and a success alert tells me that the info was posted. The problem is that when the page is opened it states that the posted name index is undefined. Here is my script to get the information:

        <script>
        $(document).ready(function()
        {
            $("table.jobs tbody td#job").click(function()
            {
                var $this = $(this);
                var col   = $this.text();
                var LeftCellText = $this.prev().text();
                if (col == '')
                alert("Please pick another column");
                else
                $.ajax(
                {
                    type:"POST",
                    url:"../php/jobUpdate.php",
                    data:"name=" + LeftCellText,
                    success: function()
                    {
                        window.location = "../php/jobUpdate.php";
                    }
                });
            });
        });
    </script>

and here is the simple php page it is sending to:

$name = $_POST['name'];
echo $name;

I am new to jQuery, and I cannot figure out why this is not working?

  • 写回答

4条回答 默认 最新

  • dqu92800 2013-06-18 17:36
    关注

    From your comment to my previous post, it seems that you don't need ajax at all. You just need a form in your HTML:

    <form id="MyForm" action="../php/jobUpdate.php" method="POST">
        <input type="hidden" id="jobID" name="yourJobID">
    </form>
    

    Note that forms are invisible until you put something visible inside them.

    You can have select controls (dropdowns) in there, or all form elements can be invisible by using hidden input fields (like the HTML just above), which you can populate using jQuery. Code to do that would look something like this:

        <script>
        $(document).ready(function() {
            $("table.jobs tbody td#job").click(function() {
                var $this = $(this);
                var col   = $this.text();
                var LeftCellText = $this.prev().text();
    
                //Set value of hidden field in form. This is how
                //data will be passed to jobUpdate.php, via its `name` param
                $('#jobID').val(LeftCellText); 
    
                if (col == '')
                    alert("Please pick another column");
                else
                    $('#myForm').submit();
            });
        });
    </script>
    

    If you add more values to your form to send over to jobUpdate.php, just ensure that each element has a name, such as <input type="text" name="selectedJobType"> (this element, type="text", would be visible on screen).

    In the jobUpdate.php file, you would get these values thus:

    $SJT = $_POST['selectedJobType'];
    $id = $_POST["yourJobID"];
    
    //Use these vars in MySQL search, then display some HTML based on results
    

    Note that the key referenced in the $_POST[] array (selectedJobType / yourJobID) is always identical to the name specified in the HTML tag. These names are case sensitive (and spelling counts, too).

    Hope this isn't TMI, just wish to cover all the bases.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?