简体   繁体   English

jQuery Ajax请求不起作用

[英]Jquery Ajax Request Not Working

I tried to send form data via a jquery ajax request to the server and display response of the php file that located on the server, in the <div id=”response-box”></div> . 我试图通过jquery ajax request将表单数据jquery ajax request到服务器,并在<div id=”response-box”></div>显示位于服务器上的php文件的<div id=”response-box”></div> But when I submit the form nothing happened. 但是当我提交表格时,什么也没发生。 What is my mistake? 我怎么了 I tried three days to solve this. 我尝试了三天来解决这个问题。 Help me. 帮我。 Thank You! 谢谢!

I tested this on localhost (wamp server) 我在本地主机(垃圾服务器)上对此进行了测试

This is my html code on index.html 这是我在index.html上的html代码

<form id="myform">
  <input type="text" id="fname">
  <input type="text" id="lname">
  <input type="submit" id="data-send-button" value="Send Data"> 
</form>

<div id="responce-box"> </div>

This is Jquery Ajax Request on app.js 这是app.js上的Jquery Ajax请求

$(document).ready(function(){

    $('form#myform').on('submit' , function(){

       $.ajax({

        url:"submit.php" , 
        type: "POST" ,
        data: {fname: $('#fname').val(), lname: $('#lname').val()} ,

        success: function(data){
        $('#responce-box').html(data);}


        });


    });

});

This is my php code on submit.php (localhost/wamp server) 这是我在submit.php(本地主机/沼泽服务器)上的PHP代码

<?php

$fname = $_POST['fname'];
$lname = $_POST['lname'];

echo $name $lname;

// Code for Data Sending to the MySql Data Base

No when I click submit button, Text Values in the text boxes are Disappeared 当我单击提交按钮时,否,文本框中的文本值消失

Looking at your code, you are using $.ajax request when clicking submit button. 查看您的代码,单击“提交”按钮时,您正在使用$ .ajax请求。 Means that, the form reacted as normal form that refresh the page. 意味着,该表单作为刷新页面的普通表单做出了反应。 If you want to request an ajax, then you should disable default behavior of the form by using preventDefault() code. 如果要请求ajax,则应使用preventDefault()代码禁用表单的默认行为。 And i did't see any error in your js code. 而且我没有在您的js代码中看到任何错误。

Change js code into this : 将js代码更改为此:

$(document).ready(function(){

$('form#myform').on('submit' , function(e){
    e.preventDefault();//<--add here ---^
   $.ajax({

    url:"submit.php" , 
    type: "POST" ,
    data: {fname: $('#fname').val(), lname: $('#lname').val()} ,

    success: function(data){
    $('#responce-box').html(data);}


    });


});

});

Some error in your php code. 您的php代码中有些错误。 Try following code : 尝试以下代码:

$fname = $_POST['fname'];
$lname = $_POST['lname'];

echo $fname.$lname;

Problem Solved. 问题解决了。 Credit goes to the people who comment/answer under this post. 感谢在此帖子下发表评论/回答的人。 Thank You! 谢谢!

Error was on the app.js file. app.js文件出错。 Added event.preventDefault(); 添加了event.preventDefault(); and added dataType :"html", to the ajax request. 并将dataType :"html",添加到ajax请求。

 $(document).ready(function(event){ event.preventDefault(); //Added $('form#myform').on('submit' , function(){ $.ajax({ url:"submit.php" , type: "POST" , dataType :"html", //Added data: {fname: $('#fname').val(), lname: $('#lname').val()} , success: function(data){ $('#responce-box').html(data);} }); }); }); 

The variable names dont look correct $name vs $fname . 变量名称看起来不正确, $name vs $fname

Also, try specifying dataType html . 另外,尝试指定dataType html $.ajax is a little tricky to get it working properly sometimes. $.ajax有时很难使其正常工作。

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

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