Solved

how to pass the output of an ajax script...

Posted on 2012-04-12
6
282 Views
Last Modified: 2012-04-12
Hi, I have an application where I use a Ajax script to display a value in a form, the value is being displayed correctly. When I submit the form I need to pass this value using $_POST, how do I do this? Thanks for the help.

A
socDate.php
ajaxScript.txt
0
Comment
Question by:aej1973
  • 4
  • 2
6 Comments
 
LVL 33

Expert Comment

by:Slick812
ID: 37839265
greetings  aej1973, , , not sure about what to help you with, this could be done with javascript (a js post form function) or you could do this by changing a form hidden element value to the   xmlhttp.responseText   string value  (of course with out the </br>)

but you seem inexperienced with AJAX, so not sure what might suit you.

could you show us your form  DHTML, so maybe I could show you how to change a hidden form element value to the  $row['admit_date'] from ajax
0
 

Author Comment

by:aej1973
ID: 37839346
Hi Slick812, attached is the from that I am using.
Planner.php
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 250 total points
ID: 37839662
OK, I looked at your code for the  planner.php and the  socDate.php   files.

First this is some code you can change in the planner.php  file -

you need to add a hidden element to the  newPatient   FORM, I  might suggest you add the hidden right after the FORM declaration-

<form name="newPatient" method="post" action="cellColor.php">
<input type="hidden" name="ajid" value="0">

Open in new window

the rest of the PHP and form inputs in the planner.php do NOT need to be changed as far as I can tell.


next you will need to change this hidden value in the Ajax showUser( ) function add this line right after the socDate change the inner HTML thing -

// You should have some error control for ajax so add -
if ((xmlhttp.responseText == "ERROR") || (xmlhttp.responseText == "")) {
    alert('Ajax ERROR - No Result');
    return;
    }
document.getElementById("socDate").innerHTML = xmlhttp.responseText+"<br>";
document.newPatient.ajid.value = xmlhttp.responseText; // add this line

Open in new window



Now for the  socDate.php  file

there are several things about this php file that are unsafe for MySQL

include "../common/connect.php";
$q = $_GET["q"];
// WARNING you must avoid SQL injection in AJAX
$q = mysql_real_escape_string($q);
$sql = "select admit_date from patient_newpatient where patient_id ='".$q."'";
$result = mysql_query($sql);
if (!$result) {
// not all selects will return results, you might also check for the number of rows returned
    echo 'ERROR';
    exit;
    }
$row=mysql_fetch_assoc($result);
///$result = mysql_query($sql);  second query is invalid

//Only send back the admit date, NO HTML <br>
echo $row['admit_date'];

Open in new window




I hope these suggestions help you, I can not try this in working PHP because I do not have your database,

the code above is untested code but should give you some code to help
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:aej1973
ID: 37839714
Thank you Slick812, I will work on this and get back to you.

A
0
 

Author Comment

by:aej1973
ID: 37841134
Thanks a ton, works great!
0
 

Author Closing Comment

by:aej1973
ID: 37841136
Thank you.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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 four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

786 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