简体   繁体   English

如何使用ajax将值传递到php脚本?

[英]How to pass the values to php script using ajax?

I have written a code to pass values of input fields to a php script using ajax but it is not working. 我已经写了一个代码,使用ajax将输入字段的值传递给php脚本,但是它不起作用。 Can anyone suggest how to rectify this code ? 任何人都可以建议如何纠正此代码? I want to display the values passed through ajax in the php file. 我想在php文件中显示通过ajax传递的值。

ex.php ex.php

<?php
$temp = $_POST['start_date'];
$name = $_POST['end_date'];
echo $temp.$name;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">


$(document).ready(function(){


$("input").change(function(){

  var fname = $("#fname").val();
   var lname = $("#lname").val();

   $.ajax({
   method: "POST",
   url: "ex.php", // path to php file
    data: { start_date: fname, end_date: lname } // send required data here
  })
  .done(function( msg ) {

  }); 


  });

  });

 </script>
 </head>
 <body>
 <form action="#" method="post" name="form1">
 <input type="text" name="fname" id="fname"/>
 <input type="text" name="lname" id="lname"/>
 </form>
 </body>
 </html>
try using below code :
<?php
$temp = $_POST['start_date'];
$name = $_POST['end_date'];
echo $temp.$name;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">


$(document).ready(function(){


$("input").change(function(){


  var fname = $("#fname").val();
   var lname = $("#lname").val();

var post = "&start_date=" + fname + "&end_date=" + lname;

   $.ajax({
                        'url': ex.php,
                        'data': post,
                        'type': 'POST',
                        'success': function (data)
                        {

                        }
                    });

  });

  });

 </script>
 </head>
 <body>
 <form action="" method="post" name="form1">
 <input type="text" name="fname" id="fname"/>
 <input type="text" name="lname" id="lname"/>
 </form>
 </body>
 </html>

Add an element to your makeup 为妆容添加元素

<span id="idOfSomeElementYouWantToUse"></span>

and in your callback set it's text. 并在您的回调集中设置为文本。

.done(function( msg ) {
    $("#idOfSomeElementYouWantToUse").text(msg);
});

Add a div to your page and use html() to append the data that comes from the ajax request 将div添加到您的页面,然后使用html()附加来自ajax请求的数据

 <body>
 <form action="#" method="post" name="form1">
 <input type="text" name="fname" id="fname"/>
 <input type="text" name="lname" id="lname"/>
 </form>
 <div class="ajax-result"></div>
 </body>

js: js:

$("input").change(function(){

  var fname = $("#fname").val();
   var lname = $("#lname").val();

   $.ajax({
    method: "POST",
    url: "other_file.php", // path to php file
    data: { start_date: fname, end_date: lname }, // send required data here
    success:function(data){
     $(',ajax-result').htm(data);
     }
  });

Note: from what i can tell you are ajaxing to the same page,i strongly suggest you create a new php file(other_file.php) with your logic in it 注意:从我可以告诉您的内容来看,我强烈建议您使用逻辑将其创建为一个新的php文件(other_file.php)

From what I can understand from your comment above, you want to insert textbox value to the database, to do this you don't want to call ajax on textChange event because at that time not all value will be present, I suggest adding submit button to the form and call ajax on form submit. 从上面的评论中我可以理解,您想要向数据库中插入文本框值,而您不想在textChange事件上调用ajax,因为当时并不能显示所有值,建议添加提交按钮到表单,并在表单提交时调用ajax。

Here is the main file: 这是主文件:

ajax_ex.php (You should name it as per your requirement) ajax_ex.php (您应该根据需要命名)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Untitled Document</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
  <form id="frmInsert" method="post" name="form1">
    <input type="text" name="fname" id="fname"/>
    <input type="text" name="lname" id="lname"/>
    <input type="submit" value="Insert" />
  </form>
  <div id="msg">

  </div>
</body>

<!-- You should put javascript at bottom -->
<script   src="jQuery-2.1.4.min.js"></script>
<script type="text/javascript">

  $("#frmInsert").submit(function(e){
    var fname = $("#fname").val();
    var lname = $("#lname").val();

    $.ajax({
    method: "POST",
    url: "insert.php", // path to php file
      data: { start_date: fname, end_date: lname } // send required data here
    })
    .done(function( msg ) {
      msg = $.trim(msg);
      if (msg == 'true') {
        $('#msg').html("Data is inserted successfully");
      }else {
        $('#msg').html("Data insertion failed");
      }
    });

    e.preventDefault(); 
  });

And you should not call the same file in ajax, I recommend that you create individual file that will be called in ajax, this file will handle all back-end processing. 并且您不应该在ajax中调用相同的文件,我建议您创建将在ajax中调用的单个文件,该文件将处理所有后端处理。

insert.php insert.php

<?php

//Check if post data is available (You should also check for particular attribute if it is available)
if (!empty($_POST)) {
  $temp = $_POST['start_date'];
  $name = $_POST['end_date'];

  //Insert Data into database
  // Your code
  $result = 'true'; //check for operation if it is success and store it in result

  //Echo result so you can check if insert is success of not in your ajax callback.
  echo $result;
}

?> ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM