Solved

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

Posted on 2011-03-15
4
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 167 total points
ID: 35137484
>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
ID: 35137529
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
ID: 35137533
0
 

Author Closing Comment

by:maccaj51
ID: 35235828
Thanks for your help!
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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)

690 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