Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6098533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:10:10+00:00 2026-05-23T13:10:10+00:00

I have a ajax call to a php script that updates my MySQL DB

  • 0

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">&nbsp;<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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:10:10+00:00Added an answer on May 23, 2026 at 1:10 pm

    Just echo it in your phpfile and use:

    document.getElementById('ajaxCatchbox').innerHTML = ajaxRequest.responseText;
    

    The responsetext from your ajaxrequest object picks up everything thats echoed… Dunno the equivalent in jquery tho

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ajax request that requests a php script that connects to my
I have a script that I wrote in PHP which scrapes data from a
I have this code that performs an ajax call and loads the results into
I am running ASP.NET MVC Beta. I have an AJAX call running through jQuery
I have coded some JavaScript to perform an ajax call in an asp.net application.
Let's say I have the following jQuery AJAX call: $.ajax({ type: POST, url: MyUrl,
As the title mentions, I have a timeout callback handler on an ajax call,
I am using asp.net MVC to develop an application that will have ajax interactions.
I have $.ajax({ url: identity, success: function(data) { ProcessIdentityServer(data) } }); When 'data' is
I have some AJAX work that brings across an additional form element that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.