Solved

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

Posted on 2012-04-12
6
306 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
[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
  • 2
6 Comments
 
LVL 34

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 34

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 how to look for a specific file type in a local or remote server directory using PHP.

617 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