无法使用jquery post()发布变量

编程入门 行业动态 更新时间:2024-10-27 12:25:14
本文介绍了无法使用jquery post()发布变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将变量从模态形式传递到另一页.我在表单中使用每个选项的id标签声明变量.

I am trying to pass variables from a modal form to another page. I declare the variables from the form with the id tags in each selection.

页面重新加载到test.php,但是无法回显任何变量.

Page reloads to test.php, however no variable can be echoed.

javascript

javascript

var id = $( "#id" ), name = $( "#name" ) $.post("jqtest/test.php", { device_id: "id", device_name: "name" }); load('jqtest/test.php');

test.php

echo $_POST['device_name']; echo $_POST['device_id'];

推荐答案

这似乎有很多问题...看来您正在使用echo $_POST['device_name'];和echo $_POST['device_id'];只是为了确保您的AJAX工作正常.如果是这种情况,则无需使用.load();.您还将发送$( "#id" )和$( "#name" )的jQuery对象,而不是这些表单元素中的值.稍后,在您的.post请求中,您将那些相同的变量用引号引起来,这将发送字符串"id"和"name".再说一遍-可能不是您要找的东西.

It looks like there's a lot wrong with this... It looks like you are using echo $_POST['device_name']; and echo $_POST['device_id']; just to ensure that your AJAX is working. If that's the case, you don't need to be using .load();. You are also sending the jQuery object of $( "#id" ) and $( "#name" ) instead of the values within those form elements. Later, in your .post request, you have those same variables in quotes, which is sending the strings "id" and "name". Again - likely not what you're looking for.

您的问题有些复杂,但我会尽力回答.试试这个JS:

Your question is a bit convoluted but I'll do my best to answer. Try this JS:

var id = $("#id").val(), name = $("#name").val(); $.post("jqtest/test.php", { device_id: id, device_name: name }, function(response) { console.log(response); });

在您的PHP文件中,使用以下代码:

In your PHP file, use this code:

echo $_POST['device_name']; echo $_POST['device_id'];

您将看到您的变量显示在浏览器的JS控制台中.使用此脚本,您已通过POST将两个变量发送到test.php,并且test.php将回显结果.添加到$ .post请求中的回调函数将在javascript控制台中显示test.php的输出.在 jQuery文档上了解有关.$post()的更多信息.

You will see your variables show up in the JS console of your browser. With this script, you've sent your two variables to test.php via POST and test.php will echo the results. The callback function added to the $.post request will display the output from test.php in the javascript console. Learn more about .$post() on the jQuery docs.

更多推荐

无法使用jquery post()发布变量

本文发布于:2023-10-12 02:55:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   jquery   post

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!