我想加载带有消息的php以在提交后隐藏表单但是event.preventDefault(); 破坏我的.php文件中的“已存在”检查

我想加载带有消息的php以在提交后隐藏表单但是event.preventDefault(); 破坏我的.php文件中的“已存在”检查

问题描述:

After submitting there must exist 3 possible states:

  1. Data saved and php message is loaded onto where the form was.
  2. Data already exists and I load a different php informing the user.
  3. An error occurred, show an alert asking the user to please try again.

This is what my submit .php looks like:

$test="select * from launchpage where email_launchPage ='$email'";
$testing=$link->query($test);
$alreadyExists=$testing->num_rows;

if($alreadyExists ==1){
    echo "<script>alert('already exists');window.history.go(-1);</script>";
} else{
    $sqlInsertListingType="insert into launchpage(email_launchPage,language_launchPage) values('".$email."','".$language."')";
    $link->query($sqlInsertListingType);
    echo "<script>alert('saved');window.history.go(-1);</script>";
}

It works fine on it's own and this is my try at an ajax request to submit:

$(document).on("submit","#form",function(event){
    event.preventDefault();
    var formURL = $(this).attr("action");
    var formType = $(this).attr("method");
    var formData = $(this).serialize();
    $.ajax({
        url: formURL,
        type: formType,
        data: formData,
        success:function(data, textStatus, jqXHR){
            console.log('went through ajax');
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log("i've ruined something");
        }
    });
});

Which also works, but because of the .preventDefault(); I "don't" know if the data was saved or not.

I have no idea what I should try to do to bring these 2 together, or if there's a better way to do this...

I'm guessing this must be rather easy to solve, I'm just missing something I haven't learned yet.

提交后必须存在3种可能的状态: p>

  1. 保存数据并将php消息加载到表单所在的位置。 li>
  2. 数据已经存在,我加载了一个不同的php通知用户。 li>
  3. 发生错误,显示 警告要求用户请再试一次。 li> ol>

    这是我提交的.php的样子: p>

      $ test =“select * from launchpage where email_launchPage ='$ email'”; 
     $ testing = $ link-&gt; query($ test); 
     $ alreadyExists = $ testing-&gt; num_rows; 
     
    if  ($ alreadyExists == 1){
     echo“&lt; script&gt; alert('已存在'); window.history.go(-1);&lt; / script&gt;”; 
    } else {
     $ sqlInsertListingType  =“插入launchpage(email_launchPage,language_launchPage)值('”。$ email。“','”。$ language。“')”; 
     $ link-&gt; query($ sqlInsertListingType); 
     echo“&lt  ; script&gt; alert('saved'); window.history.go(-1);&lt; / script&gt;“; 
    } 
      code>  pre> 
     
     

    它工作正常 在它自己,这是我的tr y在ajax请求提交: p>

      $(document).on(“submit”,“#form”,function(event){
     event.preventDefault()  ; 
     var formURL = $(this).attr(“action”); 
     var formType = $(this).attr(“method”); 
     var formData = $(this).serialize(); \  n $ .ajax({
     url:formURL,
     type:formType,
     data:formData,
     success:function(data,textStatus,jqXHR){
     console.log('through ajax');  
    },
    错误:函数(jqXHR,textStatus,errorThrown){
     console.log(“我已经破坏了某些东西”); 
    } 
    }); 
    }); 
      code  >  pre> 
     
     

    哪个也有效,但由于.preventDefault(); 我“不”知道数据是否已保存。 p>

    我不知道我应该怎样做才能将这两个数据组合在一起,或者是否有更好的方法可以做到这一点 这... p>

    我猜这一定很容易解决,我只是遗漏了一些我尚未学到的东西。 p> div>

Don't return JS or other html. return some json:

<?php

... process form data
if (processing succeeds) {
   $msg = array(
       'code' = 1,
       'message' = 'Success'
   );
} else {
   $msg = array(
       'code' => 0,
       'message' => 'Failed, something dun blowned up'
   );
}
echo json_encode($msg);

Then in your success handler:

success: function(data) {
        if (data.code == 1) {
             do_success_stuff();
        } else {
            alert('requested failed with error #' + data.code + " and message '" + data.message + "'");
        }
 }