I want to call PHP function with the help of JQuery and I also want that Jquery code catch the return type of the calling function for further processing...........
My jQuery code is:
$(document).ready(function(){
$('#ibtn_save').click(function(){
$.post('PHP/newdep.php', { dep_id: newdep.itext_depid.value }, function(result){
if(result == 'exist'){
$('#error_div').html(result).show();
}
});
});
});
My PHP Code is:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$deptid = mysql_real_escape_string($_POST['dep_id']);
$catcher = save_fxn($deptid);
echo $catcher;
}
function save_fxn($ltxt_deptid)
{
$query = "SELECT * FROM table_mstr_department WHERE DeptId='$ltxt_deptid'";
$query_result = mysql_query($query);
if (is_resource($query_result) && mysql_num_rows($query_result) > 0)
{
$sql_result = mysql_fetch_assoc($query_result);
return "exist";
}
else
{
$query = "INSERT INTO table_mstr_department (DeptId, DeptName) VALUES ('$ltxt_deptid','$ltxt_deptname')";
$output = mysql_query($query);
if (!$output)
{
echo "Data not inserted successfully!";
}
return "success";
}
}
?>
and HTML code is as
Please fix the errors below! <p> <label for="dep_id">Department Id :</label> <input id="itext_depid" name="text_depid" type="text" /> <span class="sdepid"></span> </p>
<p> <label for="dep_name">Deparment Name :</label> <input id="text_depname" name="text_depname" type="text" /> <span class="sdepname"></span> </p>
<input type="submit" id="ibtn_save" name="btn_save" value="Save">