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

  • SEARCH
  • Home
  • 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 7851997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:18:56+00:00 2026-06-02T19:18:56+00:00

I would like to save a message in PHP variable and send it back

  • 0

I would like to save a message in PHP variable and send it back with my other array variable that is already coming back. For instance, I have some error checking that takes place inside the PHP code and would like a string variable with the specific message sent back for use in my javascript.

Here is the PHP:

<?php
    include('config-searchres.php');

    $term = $_POST['resid'];
    $sql = mysql_query("SELECT * FROM ap_form_8 WHERE id = '$term'"); //select first name (element_1_1) from form #8

    if ($row = mysql_fetch_array($sql)){  //if reservation number exists
        if ($row['element_11'] != 'Cancelled'){  //if reservation has not already been cancelled
            if (strtotime($row['element_3']) >= strtotime(date("Y-m-d"))){  //if reservation has not already passed date
                echo json_encode($row);
            }
            else  //Reservation already passed (old reservation)
            {
                echo 'passed';
            }
        }
        else  //Reservation already cancelled
        {
            echo 'cancelled';
        }
    }
    else  //Reservation not found
    {
        echo 'not found';
    }
    mysql_close();
?>

As you can see, there are 3 different messages, “passed”, “cancelled”, and “not found”… if one of these conditions exists, I would like to send this string back to my javascript so I can display it in a DIV. However, I also want to send the $row data with it.

My javascript:

    <script type="text/javascript">
        $(document).ready(function(){
            resetForms('reservation');
            $('#form-reservation').submit(function(event){ 
                event.preventDefault();  //the page will no longer refresh on form submit.
                var resCheck = $(this).find('input[class="reservationid"]').val(); //now we have the reservation ID, let's perform our check.
                $.ajax({ 
                    url: 'inc/searchres.php', 
                    type: 'POST',
                    data: 'resid='+resCheck, 
                    success: function(data){  //data is all the info being returned from the php file 
                        $('#reservation-id').val(resCheck);  //add read ID back into text box
                        var jsonData = $.parseJSON(data);  //parse returned JSON data so we can use it like data.name, data.whatever 
                            //****I wanted the line just below this to display the appropriate message sent back from the PHP****
$("#res-message").html('<a>Reservation ID Located, Information is displayed below</a>');
                            $('#json-reservation').populate({personal_first_name:jsonData['element_1_1'],personal_last_name:jsonData['element_1_2'],personal_phone_1:jsonData['element_7'],personal_email:jsonData['element_2'],reservation_status:jsonData['ADD THIS CELL'], reservation_id:jsonData['id'], reservation_date:jsonData['element_3'],reservation_time:jsonData['element_4'],reservation_party:jsonData['element_5'],reservation_special_request:jsonData['element_6'],reservation_using_coupon:jsonData['element_9'],reservation_coupon_code:jsonData['element_10'],reservation_status:jsonData['element_11']});
                            $("#res-cancel-message").html('');
                    },
                    error: function(){
                        $("#res-message").html('<a>There was an error with your request</a>');
                        $("#res-cancel-message").html('');
                    }
                });
            });
        });
        </script>

I marked with asterisks where I populate the DIV with a static message at this time, this is the line where I would populate the message from PHP. Any ideas?

  • 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-06-02T19:18:57+00:00Added an answer on June 2, 2026 at 7:18 pm

    Send both in ajax. LIke
    Dont echo anything in the body of your if else, just store the message in variable, say, $message = 'passed', now do this at the end of your php request page:

    echo json_encode(array('message_js'=>$message, 'row_js' => $row));
    

    This sends json array as responce so u can send as much variables in it as much u like. Just put them in an array() and convert them into json using json_encode()
    to convert into json and pass as response. When recieved in success function of your ajax, just decode the two json variables : message_js and row_js.

    You can use parsejson of jquery to get your variables then

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

Sidebar

Related Questions

I have a plist where I would like to save a some response details
I have a model for which I would like to save only the attribute
I have created a MySQL table and would like to save the contents of
I have an ASP.Net form and i would like to save it into a
I've made some experimental code that I would like to save in the repository,
I have app engine app that uses a java servlet to save a message
I would like to save user messages to a database. The message will be
I would like to save the programs settings every time the user exits the
I would like to save data in cookies (user name, email address, etc...) but
I would like to save and restore the state of a WinForms Form ,

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.