简体   繁体   English

错误未显示在ajax提交页面上

[英]Errors doesn't show on page on ajax submit

I'm trying to make table insert new row with ajax, php, mysql. 我正在尝试使表使用ajax,php,mysql插入新行。 So far I was able to make the table and on correct data in the fields to submit and save data to database. 到目前为止,我已经能够在表中以及字段中的正确数据上提交并保存数据到数据库。

Now I'm trying to make some validations and display errors if any but I don't see errors on the page.. I see them only under developer console. 现在,我尝试进行一些验证并显示错误(如果有),但在页面上看不到错误。我仅在开发人员控制台下看到它们。

This is my insert row on the table 这是我在桌子上的插入行

<tr id="new_row">
    <td></td>
    <td><input type="text" id="new_name"></td>
    <td><input type="email" id="new_email"></td>
    <td>
         <input type="button" id="insert" value="Insert Row" onclick="insert_row()">
    </td>
</tr>

ajax part 阿贾克斯部分

function insert_row()
{
    var name=document.getElementById("new_name").value;
    var email=document.getElementById("new_email").value;
    $.ajax
    ({
          type:'post',
          url:'insert.php',
          data:{
               insert_row:'insert_row',
               name_val:name,
               email_val:email
          },
          success:function(response) {
               if(response!="")
               {
                   var id=response;
                   var table=document.getElementById("user_table");
                   var table_len=(table.rows.length)-1;
                   var row = table.insertRow(table_len).outerHTML="<tr id='row"+id+"'><td id='name:"+id+"'>"+name+"</td><td id='email:"+id+"'>"+email+"</td>";

                   document.getElementById("new_name").value="";
                   document.getElementById("new_email").value="";
               }
          }

    });
}

And PHP part insert.php PHP部分insert.php

if (isset($_POST['insert_row'])) {

     $name=$_POST['name_val'];
     $email=$_POST['email_val'];

     if($name < 4) { echo 'please enter the name';exit(); }
     if($email < 4) { echo 'please enter email';exit(); }

    $value = $pdo->prepare('SELECT * FROM client WHERE name= ?');
    $value->bindParam(1, $name, PDO::PARAM_STR);
    $value->execute();
    $result = $value->fetch();


    $value1 = $pdo->prepare('SELECT * FROM client WHERE email= ?');
    $value1->bindParam(1, $email, PDO::PARAM_STR);
    $value1->execute();
    $result1 = $value1->fetch();

    if ($result > 0) 
    {
        echo 'Name already exist';
        exit();
    }
    elseif ($result1 > 0) 
    {
        echo 'Email already exist';
        exit();
    }       
    else
    {
          // query for insert here

I see none of the errors on the page. 我在页面上没有看到任何错误。 Can you show me how to pass them? 你能告诉我如何通过他们吗?

As example you can add a div here: 例如,您可以在此处添加div:

<td>
      <input type="button" id="insert" value="Insert Row" onclick="insert_row()">
       <div id="error" style="display:none"></div>
</td>

in your ajax: 在你的ajax中:

    success:function(response) {
                       if(response!="")
                       {
                           var id=response;
                           var table=document.getElementById("user_table");
                           var table_len=(table.rows.length)-1;
                           var row = table.insertRow(table_len).outerHTML="<tr id='row"+id+"'><td id='name:"+id+"'>"+name+"</td><td id='email:"+id+"'>"+email+"</td>";

                           document.getElementById("new_name").value="";
                           document.getElementById("new_email").value="";
                          //just in case the error has been shown before
                           $('#error').hide()
                       }else{

                       $('#error').html(response);
                       $('#error').show()
                  }

I Hope this will let you go on. 希望这会让您继续前进。

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

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