AJAX to PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

AJAX to PHP

Post by manRay »

I need to send several variable from a form to a php page. It sends me an errors message that I set , if agree was not checked. Here is the link http://www.tandembyte.com/register.html I think the problem is getting the php file to notice that the check box was checked.?

Code: Select all

 
<script language="javascript">
if (window.XMLHttpRequest) { 
        XMLHttpRequestObject = new XMLHttpRequest(); 
      } else if (window.ActiveXObject) { 
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
 
      function getData(id)  
      {  
        if(XMLHttpRequestObject) { 
          var obj = document.getElementById(id);  
          XMLHttpRequestObject.open("POST", "../php/registernext.php");  
          XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');  
          
           XMLHttpRequestObject.onreadystatechange = function()  
          {  
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {  
                obj.innerHTML = XMLHttpRequestObject.responseText;  
            }  
          }
         
          XMLHttpRequestObject.send("fname=" + document.form1.elements["fname"].value, "lname=" + document.form1.elements["lname"].value, "password=" + document.form1.elements["password"].value, "password2=" + document.form1.elements["email"].value, "email=" + document.form1.elements["email"].value, "email2=" + document.form1.elements["email2"].value, "operating=" + document.form1.elements["os"].value, "agree=" + document.form1.elements["agree"].value);
        } 
      } </script>
 
Last edited by Benjamin on Sat May 09, 2009 10:09 am, edited 1 time in total.
Reason: Changed code type from text to javascript.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: AJAX to PHP

Post by jazz090 »

the problem is the fact that you are refrencing the objects document.form1.elements["agree"].value and thats not the way to go. use document.form1.elements["agree"].checked to send a true or false value to PHP and this is very important: PHP wont access them as booleans but as strings so :

Code: Select all

if ($_GET['agree'] == "true") {
    // throw error //
}
notice how i have TRUE in quotes, that is essential in this. Also if i were you, i would send my data using POST and not GET.
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: AJAX to PHP

Post by manRay »

it still give the same error.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: AJAX to PHP

Post by jazz090 »

what error?
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: AJAX to PHP

Post by jazz090 »

ok if you are sending the data as POST, just access it with POST['agree'] == "true"
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: AJAX to PHP

Post by manRay »

please make sure you agree with the terms of use before continuing, which I set if the check box was not checked. But when I check it, this error pops up.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: AJAX to PHP

Post by jazz090 »

send the php code that you are using to acess 'agree'
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: AJAX to PHP

Post by manRay »

Code: Select all

 
<?php
  include ('*****.php');
  
  $testfname = $_POST['fname'];
  $testlname = $_POST['lname'];
  $testpassword = $_POST['password'];
  $testpassword2 = $_POST['password2'];
  $testemail = $_POST['email'];
  $testemail2 = $_POST['email2'];
  $operating = $_POST['os'];
 
//make sure user agrees to T.O.S.
if ($_POST['agree'] == "true") {
            
    //test for duplicate email
    $query = "SELECT * FROM ***** WHERE email='$testemail'";
    $result = mysql_query($query);
    $num = mysql_num_rows($result);
            
        if ($num == 0) {
                
            //make sure emails and passwords match up
            if (($testpassword == $testpassword2) && ($testemail == $testemail2)) {
                 
                //create random code
                $confirm_code = md5(uniqid(rand()));
                            
                //strip fields of spaces
                $fname = strip_tags($testfname);
                $lname = strip_tags($testlname);
                $password = strip_tags($testpassword);
                $email = strip_tags($testemail);
                            
                //insert into database
                $sql = "INSERT INTO **** SET code='$confirm_code', fname='$fname', lname='$lname', password='$password', email='$email', system='$operating'";
                $result = mysql_query($sql);
                            
                if ($result) {
                    $message.= "Your confirmation link \r\n";
                    $message.= "Click on this link to activate your account \r\n";
                    $message.= "http://www.tandembyte.com/php/activated.php?passkey=$confirm_code\r\n";
                    $message.= "Email: $email\r\n";
                    $message.= "Password: $password";
                    $sentmail=mail("$email",'Registration Conformation',"$message");
                }   
                                                 
                // if your email succesfully sent
                            
                if($sentmail){
                    header("Location:../confirmation.html"); 
                                
                    }   
                            
                else {
                      echo "Unable to send confirmation link to your e-mail address";
                     }
                 
                }
                else {
                echo "Either password or email do not match!";
                }
            
                    }
                    else {
                        echo "This email is already in use"; 
                        }
      
 }
else {
    echo "please make sure you agree with the terms of use before continuing"; }
?>
 
Last edited by Benjamin on Sat May 09, 2009 11:15 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: AJAX to PHP

Post by jazz090 »

are you sure that agree is even getting passed into the script?
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: AJAX to PHP

Post by manRay »

yes before, I had ajax on the registration form, it worked perfectly. But I want to include ajax to the form so that it is cleaner.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: AJAX to PHP

Post by jazz090 »

theres a bug in your script, nothing code wise, sorry i cant help pu any furthur
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: AJAX to PHP

Post by manRay »

OK that worked. but when it tries to send the confirmation email, it errors there. I took a look at my database, and saw that the only variable being sent was fname.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: AJAX to PHP

Post by Yossarian »

Maybe this is not true in your case but watch out what JS sends as boolean true or false, I noticed on the library I used that it sends the STRING "true".

var_dump( $_POST ) to be double sure esp with Ajax.
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: AJAX to PHP

Post by manRay »

How can I send multiple variables from ajax to php using POST?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: AJAX to PHP

Post by John Cartwright »

When building the request string, you forgot to include ampersand between variables, i.e.

Code: Select all

fname=John&lname=Doe&email=johndoe@gmail.com
Post Reply