Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AJAX将变量发送到PHP_Php_Jquery_Ajax - Fatal编程技术网

使用AJAX将变量发送到PHP

使用AJAX将变量发送到PHP,php,jquery,ajax,Php,Jquery,Ajax,我有一个变量,我想发送到PHP代码,它位于代码的顶部,但我不断得到一个错误和未定义的dtotal是变量名,它包含一个数字。所有这些代码都在同一页中,因此我将发布到同一页 $('#emailVerzenden').click(function() { $.ajax({ url: "content.php", type: "post", data: ({ totaal: dTotaal }),

我有一个变量,我想发送到PHP代码,它位于代码的顶部,但我不断得到一个错误和
未定义的
dtotal
是变量名,它包含一个数字。所有这些代码都在同一页中,因此我将发布到同一页

$('#emailVerzenden').click(function() {
    $.ajax({
        url: "content.php",
        type: "post",
        data: ({
            totaal: dTotaal
        }),
        success: function(msg) {
            alert('Data saved: ' + msg);
        },
        error: function(error) {
            alert("couldnt be sent " + error); 
        }
    });
在我的页面顶部,我有这个代码。我不确定这是否正确,我对这方面还不熟悉

if(isset($_POST['emailVerzenden']))
{
    $totaal = $_POST['totaal'];
    var_dump($totaal);
}

我想要的是将
totaal
数据的值放入
$totaal
中,但这不起作用。数据未被发送。我在PHP代码中不断收到错误
alert()

,您正在检查是否存在一个变量以使用另一个变量。对我来说,应该是:

if(isset($_POST['totaal']))
{
$totaal= $_POST['totaal'];
var_dump($totaal);
}

您的做法是正确的,但是将
PHP
代码与
jQuery
代码分开,这样您就可以完全控制异步处理数据

index.php

  $('#emailVerzenden').click(function()
        {
            $.ajax
            ({
                url: "content.php",
                type: "post",
                data:{totaal: dTotaal},
                success: function(msg)
                {
                    alert('Data saved: ' + msg);
                },
                error: function(error)
                {
                    alert("couldnt be sent ".error); 
                }
            });
 if(isset($_POST))
  { 
    $totaal= $_POST['totaal'];
   var_dump($totaal);
 }
在php文件中,首先检查是否设置了
$\u POST
数据

content.php

  $('#emailVerzenden').click(function()
        {
            $.ajax
            ({
                url: "content.php",
                type: "post",
                data:{totaal: dTotaal},
                success: function(msg)
                {
                    alert('Data saved: ' + msg);
                },
                error: function(error)
                {
                    alert("couldnt be sent ".error); 
                }
            });
 if(isset($_POST))
  { 
    $totaal= $_POST['totaal'];
   var_dump($totaal);
 }

提到你想用html发送的数据并给它一个ID

 <div id="total"> HERE COMES THE VARIABLE YOU WISH TO SEND </div>

希望这能解决你的问题。祝你好运

看起来好像我没有使用prevent default(),这就是它不起作用的原因

$('#emailVerzenden').click(function(e)
        {
            cms=$('#sCms').val();
            templates= $('#templates').val();
            onderdelen = $('input:checkbox:checked').map(function() {
    return this.value; 
}).get();
            email = $('#email').val();
            e.preventDefault();
            $.ajax
            ({
                type: "POST",  
                url:"test.php", 
                data:{postEmail : email,postOnderdelen : onderdelen,postCms : cms,postTotaal : postTotaal, postTemplates : templates}, 
                success: function(rs)   
                {
                    alert("Data saved:" + rs);
                },
                error: function(error)
                {
                    alert("couldnt be sent" + error); 
                }
            }); 
            e.preventDefault(); 
        });     

首先定义变量dtotal。如果您得到错误
alert()
,则表示服务器端代码没有返回
200 OK
响应。检查控制台的“网络”选项卡以查看响应和错误。还请注意,JS中的字符串连接运算符是
+
,而不是
,正如您在
错误处理程序中使用的那样。@SantoshPatel其定义值来自Jquery中的数学运算。当我提醒它时,它起作用了,我也用它作为输出作为价格,唯一的问题是它没有发布。@Rorymcrossan在我签出它之前从未使用过网络选项卡,但你能给我一个我应该寻找的线索吗。我正在做的是一个数学运算,我想将结果作为变量发送给PHP,当我提醒它时,我得到值600,但当我尝试发送它时,它不起作用。将值发送到一个变量中,如
var total='your result'
,然后像我发送的那样发送。是的,这就是我想告诉你的,我在数据总计中得到了值/结果。soo dtotal是值。
$.post('content.php',{'dtotal':dtotal},函数(response){if(response='YES'){})
您将通过
$this->input->post('dTotaal')在控制器中获取此变量
我的两个代码都在同一个文件content.php中,php在顶部,Jquery在底部。我得到了它,但删除了文件顶部的php代码,并将其放在另一个文件中
[newfile.php]
。不要忘记将jQueryAjax中的
url
更改为
url:“newfile.php”
做了更改,但仍然是一样的,问题在于ajax代码。它根本没有发布任何东西。我一直收到错误警报()。