i have this code which ask user to input value and submit.
html
<form id="myForm" method="post" action="saving.php">
<p><label for="input1">Input 1:</label>
<input type="text" size="10" name="input1" id="input1" />
</p>
<input id="save" name="save" type="submit" value="save" />
</form>
<p><span id="thevalue"></span><span id="existornot"></span></p>
js
$("#myForm").submit(function(event) {
event.preventDefault();
$("#myForm").validate(
{ rules: {input1: "required",},
messages: {input1: "message error input"},
});
if( $("#myForm").valid()){
var $form = $("#myForm" ),
url = $form.attr( "action" ),
insertedVal=$("#input1").val();
$.post(url,{input1:insertedVal},
function(){
//displaying value condition
$("#thevalue").html(insertedVal);
// ... msg from php to be inserted to #existornot
});
}
});
saving.php
<?php
if(!empty($_POST['input1']))
{
$inputone= mysql_real_escape_string(trim($_POST['input1']));
$result = mysql_query("SELECT COUNT(*) FROM `test_table` WHERE `name` = '$inputone' ");
if (mysql_result($result, 0, 0) > 0)
{
echo "is duplicate data" ;
}
else
{
mysql_query("INSERT INTO `test_table`(name) VALUES ('$inputone')") ;
echo "is updated to db" ;
}
}
else
{
echo "could not be empty" ;
}
?>
How to pass those message of 'is duplicate data' or ' is updated to db' from the php to the javasript, to tell user that the submit process is fail or success ?
Thanks in advance for any advice.