I have a ajax call to a php script that updates my MySQL DB with a new record when the user submits a data string as an entry. Part of the PHP is an if/else in place that will check to see if the entry is a duplicate. I can stop the duplicate from updating the database but I don't know how to send back an "error" message that says "That record already exists"...here is my jquery: [script] $(function() {
$(".entry_button").click(function()
{
var element = $(this);
var boxval = $("#entry").val();
var dataString = 'entry='+ boxval;
if(boxval=='') {
alert("Please Enter Some Text");
} else {
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="images/loading.gif" align="absmiddle"> <span class="loading">Broifying It...</span>');
$.ajax({
type: "POST",
url: "update_data.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").prepend(html);
$("ol#update li").slideDown("slow");
document.getElementById('entry').value='';
$("#flash").hide();
}
});
}
return false;
});
});
[/script]
and here is my php script that updates the DB (update_data.php):
[php]
include('db.php');
if(isset($_POST['entry']))
{
$date_created = date('Y-m-d H:i:s');
$entry = $_POST['entry'];
$query = "SELECT COUNT(*) AS count FROM table WHERE entry = '$entry'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if ($row['count'] == 0) {
mysql_query("insert into table (entry,date_created) values ('$entry','$date_created')");
$sql_in= mysql_query("SELECT entry,id FROM table order by id desc");
$r=mysql_fetch_array($sql_in);
$newentry=$r['newentry'];
$id=$r['id'];
?>
<li class="entry bar<?php echo $id; ?>">
<div class="thebro"><span><?php echo $newentry; ?></span></div>
<div class="hr"></div>
<div class="utility-left"><span class="share"><a href="#" title="Share">share</a></span> | <span class="time">days ago</span></div>
<div class="utility-right"><span class="comments">COMMENTS</span> | <span class="like">LIKE</span></div>
</li>
<?php
} else {
$error = "Sorry, that record already exists";
}
}
[/php]
I want to be able to spit that $error
variable from the php script to be sent back in the ajax call and be displayed in a element in my HTML. I've googled the crap out of ajax error messages but they all have to do with header errors, not validation error messages. I also am not using json_encode, unless someone wants to redo that jquery chunk above so that it is sent as json data.
Any ideas would be awesome!