BootstrapFormjQuery$.post不刷新页面和PHP发送邮件

Bootstrap Form jQuery $.post without refreshing page and PHP send mail

本文关键字:PHP post 刷新 BootstrapFormjQuery      更新时间:2023-09-26

我已经在代码中定义了所有参数,但无法解决错误。注意:第3行C:''examplep''htdocs''test''process.php中的未定义索引:fname。我有定义,但钢得到了这个错误。从表单中删除第一个字段时,rest工作正常。下面是我的引导程序index.php

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script>
 
$(document).ready(function(){
 
$("#submit").click(function(){   
 
 
    var  postname = $("#fname").val();
       
    var postemail = $("#email").val();
       
    var postphone = $("#phone").val();
       
       
       
      $.post("process.php", {fname:postname, email:postemail, phone:postphone }, function(postresult) {
       
      $("#postdiv").html(postresult);
 
    });
}); 
 
 
});
 
</script>
	<style type="text/css">
		.box {
			width: 900px; 
			background: gray;
			color: white;
			margin: 0 auto;
			padding: 10px;
			text-align: center;
		}
		#postdiv{
    margin-left: 50px;
    width:30%;
    background-color:#eee;
}  
	</style>
    </head>
<body>
<div class="container">
<h1>Bootstrap Form jQuery $.post without refreshing page  </h1>
    <form class="form-horizontal" role="form">
      <div class="form-group">
        <label for=""  class="col-sm-2 control-label">Name:</label>
        <div class="col-sm-5">
          <input type="text" name="fname" class="form-control" id="fname" placeholder="Enter Name">
        </div>
      </div>
        
      <div class="form-group">
        <label for=""  class="col-sm-2 control-label">Email:</label>
        <div class="col-sm-5">
          <input type="email" name="email" class="form-control" id="email" placeholder="Enter Email">
        </div>
      </div>
        <div class="form-group">
        <label for="" class="col-sm-2 control-label">Phone:</label>
        <div class="col-sm-5">
          <input type="text" name="phone" class="form-control" id="phone" placeholder="Enter Phone">
        </div>
      </div>           
    </form>
   </div> 
<div class="col-sm-offset-2 col-sm-5">   
<button id="submit" name="submit" class="btn btn-lg btn-block btn-warning">Submit</button>
</div><br /><br /><br />
<div id="postdiv"><h3>You entered following data!</h3></div>
 </body>
</html>

下面是我的流程.php

         <?php  
if (isset($_POST['submit'])) {  

$fullname = $_POST['fname'];
$email = $_POST['email'];
$phone = $_POST['phone'];   

$to = "davidmasihxyz@gmail.com";
$subject = "Contact Query For Order Form";
$message = "Your Name:  $fname 'n".
           "Your Email:  $email 'n" .
           "Your Phone No:  $phone 'n" ;

mail($to, $subject, $message, "From: " . $email);

echo "Form Submited Successfully..!";
}
else{
    echo "Message Failed..!";
}

?>

请帮助Jquery正常并解决问题,但无法发送邮件。我想if(isset($_POST['submit']{……}

传递给$.post的对象具有请求中发送的所有键/值对。

你放

{name:postname, email:postemail, phone:postphone }

代替

{fname:postname, email:postemail, phone:postphone }

fname上有一个打字错误。


要解决if (isset($_POST['submit']))的问题,请确保在您的请求中包括submit

{fname:postname, email:postemail, phone:postphone, submit:true}

根据我的理解。您的页面被提交为提交按钮发布所有值。你只需要根据给定的打击添加return false。您的代码应该可以工作。

$(document).ready(function(){
$("#submit").click(function(){   

    var  postname = $("#fname").val();
    var postemail = $("#email").val();
    var postphone = $("#phone").val();

      $.post("process.php", {fname:postname, email:postemail, phone:postphone }, function(postresult) {
      $("#postdiv").html(postresult);
    });
return false;
}); 

});