首页 > 解决方案 > bootstrap modals put values in database

问题描述

I have a modal with an input to add a new category and at the same time to check if this value is already in my database and show me a message.

the main code is this Javascript

$(document).ready(function() {
  $("#savebutton").click(function() {
    var name_category = $("#new_category").val();
    if (name_category != "") {
      alert("has values");
      $.post("action.php", {
        suggestion: name_location
      }, function(data, status) {
        $("#test").html(data);
      });
    } else {
      alert("give category");
    }
  });
});

and the PHP code is

$connection = sqlsrv_connect($server, $cinfo);
if( $connection === false )
{
    echo "Error (sqlsrv_connect): " . print_r(sqlsrv_errors(), true);
}

if(isset($_POST['suggestion']))
{                       
    $query = "SELECT jacat_title FROM JobAdCategories WHERE jacat_title='".$_POST['suggestion']."'";
    $statement=sqlsrv_query($connection,$query);                        
    if(sqlsrv_num_rows($statement) > 0) {
        echo"Yes";                              
     } else {
         echo"No";
    }                           
}
sqlsrv_close($connection);

标签: javascriptphpjqueryhtml

解决方案


尝试在您的 ajax 中使用 async false 。

$.post("action.php", {
    suggestion: name_location,
     type: 'POST',
     async: false //blocks window close
  }

希望它可以帮助你


推荐阅读