Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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_Ajax - Fatal编程技术网

如何使用ajax将数据传递到php?

如何使用ajax将数据传递到php?,php,ajax,Php,Ajax,我想根据使用AJAX传递给服务器的类名显示一条消息。我是新来的阿贾克斯,我不知道 myhtml代码: <div class="header"> <h2>Configuration</h2> <p> Enable: </p> <i class="fas fa-toggle-off " id="enable-btn"></i> <span id="demo">

我想根据使用AJAX传递给服务器的类名显示一条消息。我是新来的阿贾克斯,我不知道

myhtml代码:

<div class="header">
        <h2>Configuration</h2>
        <p> Enable: </p> <i class="fas fa-toggle-off " id="enable-btn"></i>
        <span id="demo">Dashboard Enabled</span>
    </div>
ajax代码:

function displayMessageAccordingToButtonState() {
    var x = document.getElementById('enable-btn').className;
    if( x == 'fas fa-toggle-off'){
        var msg = "Button Disabled"
        $('.header').load('request.php',{"displayMsg":msg});
    }
    else {
        var msg = "Button Enabled"
        $('.header').load('request.php',{"displayMsg":msg});
    }

}
php代码:

<?php
   if( $_REQUEST["displayMsg"] ){
      $msg = $_REQUEST['displayMsg'];
      echo "".$msg ;
   }
?> 

在JS中

var request = $.ajax({
        async: false,
        url: 'Php.file', / your php file path
        type: "POST",
        data: "Value1=your value";
        datatype: 'html',
    });
    request.done(function(data) {
        //data you will handle from php file
    });
在PHP中接收

$data = $_POST['data'];
  //you can check like this
  if(empty($data))
  {
    echo("none received");
  }
  else{
   echo 'passed parameter '+$data;
   //in here you will receive as data in js file
you will receive in data in js as 'passed parameter+ your value'
  }
这是工作演示

复制并尝试一下

我添加了额外的span和ajax文件内容,并将其复制到该内容中

 <html>
      <head>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
      </head>
      <body>

      <div class="header">
         <h2>Configuration</h2>
         <p> Enable: </p> <i class="fas fa-toggle-off " id="enable-btn"></i>
         <span id="demo">Dashboard Enabled</span>
     </div>
    <span id="demo2"></span>
      </body>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">          </script>
 <script>
$(document).ready(function () {
    $('#enable-btn').click(function () {
        $( ".dashboard, #demo" ).toggle();
        $(this).toggleClass("fa-toggle-off fa-toggle-on");
    });
}); 


 $('#enable-btn').click(function () {   
 var x = document.getElementById('enable-btn').className;    
 if( x == 'fas fa-toggle-off'){
    var msg = "Button Disabled"
    $('#demo2').load('request.php',{"displayMsg":msg},function(responseTxt,      statusTxt, xhr){
    if(statusTxt == "success")
        console.log("External content loaded successfully!"+responseTxt);
    if(statusTxt == "error")
        console.log("Error: " + xhr.status + ": " + xhr.statusText);
});
}
else {
    var msg = "Button Enabled"
            $('#demo2').load('request.php',{"displayMsg":msg},function(responseTxt, statusTxt, xhr){
    if(statusTxt == "success")
        console.log("External content loaded successfully!"+responseTxt);
    if(statusTxt == "error")
        console.log("Error: " + xhr.status + ": " + xhr.statusText);
});
}
 });
 </script>
 </html>

首先是创建一个包含jQuery的HTML文件

假设您有一个隐藏的输入字段

现在可以使用脚本
$(“#mydata”).val()从隐藏字段获取数据

您现在可以按如下方式编写ajax代码

$.ajax({ 
  type: 'post',
  url: '/path/to/ajax.php',
  data: {
    postvariable: $("#mydata").val()
  },
  success: function() {
    alert('success');
  }
});
在ajax.php文件中,您现在可以以
$\u POST['postvariable']

你现在可以申报了
$myphpvar=$\u POST['postvariable']

在哪里
echo$myphpvar//将打印不带引号的“sampledata”

总而言之,这就像提交一个带有post方法的表单一样。除非使用ajax,否则不需要重新加载页面

您可以在我的教程中阅读更多关于此的内容:

Read$.jquery中的ajax方法使用两个不同的简单页面或发送一些数据以检入服务器如何将元素的类名传递给php?比如数据:“value1=enable-btn.className”?是@anamolsrest您的右侧“value1=enable-btn.className”它说$ajax不是函数$。ajax不是$ajax。您是否将jquery库添加到html中?控制台说$load不是函数请检查jquery文件是否链接?我已经成功地测试了这个代码,你也可以提供php代码,或者你正在使用和我相同的代码?它的意思是:注意:未定义的索引:displayMsg在C:\xampp\htdocs\Custom Dashboard\request.php的第2行检查你的php文件变量,或者复制我粘贴的文件代码
  if( $_REQUEST["displayMsg"] ){
     $msg = $_REQUEST['displayMsg'];
     echo "".$msg ;
  }
$.ajax({ 
  type: 'post',
  url: '/path/to/ajax.php',
  data: {
    postvariable: $("#mydata").val()
  },
  success: function() {
    alert('success');
  }
});