Solved

with jquery/ajax how do I run a php script in the same file?

Posted on 2011-03-15
4
247 Views
Last Modified: 2012-05-11
I have this code at the moment which posts to a external php file...

<script>
    $(document).ready(function (){
         $('form[name=form1]').submit(function (){
                $.post("/forms/yes-reference-process.php", $('form[name=form1]').serialize(), function(data){$('#msg').html(data);});
                return false;
         });
    });
</script>
<h2>Enter Booking Reference</h2>
<h3>You should have been told or emailed this by our staff . </h3>
<form id="form1" name="form1" method="post" action="">
<table width="300" border="0" cellspacing="0" cellpadding="5">
  <tr>
    <td><label for="last_name">Last Name</label></td>
    <td><input name="last_name" type="text" id="last_name" /></td>
  </tr>
  <tr>
    <td><label for="reference">Reference</label></td>
    <td><input name="booking_reference" type="text" id="booking_reference" /></td>
  </tr>
    <tr>
    <td colspan="2">For example : 213423</td>
    </tr>
  <tr>
    <td colspan="2"><input name="submit_reference" type="submit" value="Find My Booking"  id="submit"></td>
    </tr>
</table>
</form>
<div id='msg'></div>

how do i add the php to the top and no have to post to external?
0
Comment
Question by:maccaj51
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 167 total points
Comment Utility
>and no have to post to external?

you can't... you need to send the field name/value of the form to the page. so you need to use ajax ($.post(, $.get(, $('#msg').load(
0
 
LVL 19

Assisted Solution

by:Bardobrave
Bardobrave earned 167 total points
Comment Utility
You can call your yes-reference-process.php file from $.post with a querystring builded getting values on <form> fields

$.post("/forms/yes-reference-process.php?" + $("#last_name").val() + "&" + $("#booking_reference"), $('form[name=form1]').serialize(), function(data){$('#msg').html(data);});

The problem here is that on your page load, yes-reference-process.php will be fired with the data contained inside those fields at this moment, when they probably are empty
0
 
LVL 3

Assisted Solution

by:pius_babbun
pius_babbun earned 166 total points
Comment Utility
0
 

Author Closing Comment

by:maccaj51
Comment Utility
Thanks for your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now