?
Solved

php ajax problem

Posted on 2010-01-05
6
Medium Priority
?
350 Views
Last Modified: 2013-12-13
Hi There,
i am new to AJAX within PHP, i have found a script that creates a captcha and validates this captcha with ajax.

the user enters into the form the captcha and the backend php script checks that the entered text and the text in the image are the same.

if the user enters the correct code, a message is echo'ed back confirming this and if the wrong code is used a different message.  this part of the form is working perfectly.

what i would like to be able to do is to pass to the backend php script the other fields from the form so that i can then enter them into a database.

the captcha text box in the form is called txtCaptcha and an example of another field i need to pass back is name.

my attempt to test passing back another field from the form is:
$name = $_REQUEST["name"];

thanks for your help!
<?php
//Continue the session
session_start();

//Make sure that the input come from a posted form. Otherwise quit immediately
if ($_SERVER["REQUEST_METHOD"] <> "POST") 
 die("You can only reach this page by posting from the html form");

//Check if the security code and the session value are not blank
//and if the input text matches the stored text
if ( ($_REQUEST["txtCaptcha"] == $_SESSION["security_code"]) && 
    (!empty($_REQUEST["txtCaptcha"]) && !empty($_SESSION["security_code"])) ) {
  
  $name = $_REQUEST["name"];
  
  echo "$name";
  
  echo "Thank you for your comments, we try to respond to every comment though the huge volume received means that we cannot guarantee a responce.";
  //
} else {
  echo "<h1>Test failed! Try again!</h1>";
}
?>

Open in new window

0
Comment
Question by:alexanderfoti
  • 3
  • 3
6 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 26182776
Post your html form - the html where you are executing your ajax.
0
 
LVL 1

Author Comment

by:alexanderfoti
ID: 26183129

<form id="frmCaptcha" name="frmCaptcha">
                <table>
                  <tr>
                  	<td>Name</td>
                    <td><input id="name" type="text" name="name" value="" /></td>
                  </tr>
                  <tr>
                  	<td>Email Address</td>
                    <td><input name="email" type="text" /></td>
                  </tr>
                  <tr>
                    <td>Message</td>
                    <td><textarea name="message" cols="50" rows="10"></textarea></td>
                  </tr>
                  <tr>
                    <td align="left">
                      <label for="captcha">Captcha</label>
                    </td>
                    <td>
                      <input id="txtCaptcha" type="text" name="txtCaptcha" value="" maxlength="10" size="32" />
                    </td>
                  </tr>
                  <tr>
                    <td></td>
                    <td><img id="imgCaptcha" src="create_image.php" /></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td>
                      <input id="btnCaptcha" type="button" value="Contact us" name="btnCaptcha" onclick="getParam(document.frmCaptcha)" />
                    </td>
                  </tr>
                </table> 
                
                <div id="result">&nbsp;</div>
                </form>

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 26183728
I thought you said you are using ajax. If you are, then where is the part that is sending the captcha info to the server? Bascically, it sounds like you are using ajax to send the captcha, but not the other data. So, where are your doing this?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:alexanderfoti
ID: 26183794
sorry, i wasn't very clear...

the script i am using uses ajax to post the captcha and check to see if it correct.  i am now looking to also post the rest of the form data so that i can process it.

on the page where the HTML form is i have included the java script for the AJAX.


//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}
//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();
//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 

   //Add HTTP headers to the request
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}
//Called every time our XmlHttpRequest objects state changes
function updatePage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {
   //Set the content of the DIV element with the response text
   document.getElementById('result').innerHTML = receiveReq.responseText;
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'create_image.php?' + Math.random();
 }
}
//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = 'captcha.php';
 //Set up the parameters of our AJAX call
 var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr);
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 26185182
>>the script..
That's what I thought, but if you do not post the relevant code I can't help you. What you posted last is what I was looking for.


//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = 'captcha.php';

 //Set up the parameters of our AJAX call
 //var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
 //instead of the above, you now need to iterate over all the form elements
 var postStr="";
 for( var i=0, limit=theForm.elements.length; i < limit; ++i)
 {
 	postStr+= '&'+theForm.elements[i].name + "=" + encodeURIComponent( theForm.elements[i].value );
 }
 postStr=postStr.substring(1);
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr);

}

Open in new window

0
 
LVL 1

Author Comment

by:alexanderfoti
ID: 26189533
thanks a lot for your help
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 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