weixin_33694620 2016-04-08 05:00 采纳率: 0%
浏览 16

使用Ajax发送表单(PHP)数据

I want to send a form data with Ajax after validation

About validation all think that's work

but about send data I have some problems

Ajax code :

function submitForm(){

    var server = 'http://localhost/test'; // Your PHP file
    var commentName = $('#commentName').val(); // The values of your form
    var commentMail = $('#commentMail').val(); // The values of your form
    var commentPhone = $('#commentPhone').val(); // The values of your form
    var comment = $('#comment').val(); // The values of your form

    $.ajax({ // Here the magic starts
        url: server+"/contact.php", // Where this function will send the values
        type:"POST", // To get the status of your php file
        data: "action=insertNews&commentName="+commentName+"&commentMail="+commentMail+"&commentPhone="+commentPhone+"&comment="+comment, // The values
        success: function (data){ // After sending the values to your php file you will receive number 1 or 2, if you receives number 1 it means sucess, but if you receives number 2 it means fail.
            if(data == '1'){    
                 alert("Is send");
            }
            else{
                alert("Oop");
            }
        }
    });
}

Code PHP And HTML :

<?php
if(isset($_POST['submit'])){
$message = '';
$email = '';
$name ='';


$message = $_POST['name'];
$email = $_POST['mail'];
$name = $_POST['phone'];

$to = "test@test.com";

$subject = 'New Message';

$message = " Le nom : ".$name."<br><br>".$message."<br><br> Email : ".$email;

$header = "$email";

if(mail($to, $subject, $message, $header)){
  echo '1';
}
else{
  echo '2';
}}
?>
<html>
    <head>
        <title>Test</title>
        <meta charset="UTF-8">
        <script type="text/javascript" src="js.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    </head>
    <body onload="randNums()">
        <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
            <input id="commentName" onkeyup="validateName()" name="name" type="text" placeholder="Name"><label id="commentNamePrompt"></label><br>
            <input id="commentMail" onkeyup="validateMail()" name="mail" type="text" placeholder="Mail"><label id="commentMailPrompt"></label><br>
            <input id="commentPhone" onkeyup="validatePhone()" name="phone" type="text" placeholder="Phone"><label id="commentPhonePrompt"></label><br>
            <textarea id="comment" onkeyup="validateComment()" name="commente" placeholder="Messege here"></textarea><label id="commentPrompt"></label><br> 
            <span id="digit1"></span> + 
            <span id="digit2"></span> = 
            <input id="captcha" size="2" onkeyup="validateCaptcha()"><label id="captchaPrompt"></label><br>
        </form>
    <button name="submit" onclick="validateCommentForm()" > Send</button><label id="commentFormPrompt"> </label>
    </body>
</html>

I want to know is that's right ? and if I have a problem can you fixed ?

</div>
  • 写回答

3条回答 默认 最新

  • weixin_33691598 2016-04-08 05:14
    关注

    Just use data sending in this format

     data:{action:'insertNews',commentName:commentName,commentMail:commentMail},
    
    评论

报告相同问题?

悬赏问题

  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里