0

I'm simply trying to read in a field from a text box, pass the value via ajax to PHP and then store the data in a mysql table. The table is already set up and ready to go, all I need is to insert values.

Here is my js file

    var address = $("input#email").val(); 
    //If submission is valid 
    if(submitForm == "true"){
        $.ajax({  
            url: "bin/process.php",  
            type: "POST",  
            data: {email: address},   
            success: function(response) {  
            $('#message').html(response); 
            }
        });  
        return false; 
    }

And then my PHP looks like this

    <?php

       function process($email){

        //Connect to the database  
        $con = mysql_connect('localhost', 'root', '') ; 
        if(!$con){ 
          retun mysql_error();  
        }

        //Insert Data into the Table
        $sql = 'INSERT INTO Users(email) VALUES ('$email')' ; 
        $query = mysql_query($sql, $con) 
        if(!$query){
          return mysql_error();
        }

        return "Thank you for your submission!"; 
       }

       echo process(trim($_POST['email']));
     ?>

Right now I am getting this error

Parse error: syntax error, unexpected T_STRING /opt/lampp/htdocs/mysite/bin/process.php on line 3

So it appears that I am not passing the address value correctly, but I cannot figure out why. For reference I was following the instructions on this post: Jquery ajax call from javascript to PHP

I don't quite understand the last echo line, but I tried multiple methods, and they all gave me the same error.

Thanks in advance!

Community
  • 1
  • 1
Dan
  • 127
  • 1
  • 7
  • 1
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin May 01 '13 at 13:10
  • string concatenation uses `.` ie `echo 'var is:'.$var;` – Waygood May 01 '13 at 13:11

3 Answers3

3

You spelled function wrong: funtion process($email){

It should be function process($email){

Any decent text editor or IDE would catch that instantly for you.

John Conde
  • 217,595
  • 99
  • 455
  • 496
3

I found 3 errors in your code

  1. retun mysql_error(); // return spelling wrong
  2. $sql = 'INSERT INTO Users(email) VALUES ('$email')' ; // syntax error
  3. $query = mysql_query($sql, $con); // semicolon missing

corrected code is...

<?php
       function process($email){

        //Connect to the database  
        $con = mysql_connect('localhost', 'root', '') ; 
        if(!$con){ 
          return mysql_error();  
        }

        //Insert Data into the Table
        $sql = "INSERT INTO Users(email) VALUES ('$email')" ; 
        $query = mysql_query($sql, $con);
        if(!$query){
          return mysql_error();
        }

        return "Thank you for your submission!"; 
       }

       echo process(trim($_POST['email']));
     ?>
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
1

Change your query from single qoutes to this

 $sql = "INSERT INTO Users(email) VALUES ('$email')" ; 

Because single quotes won't parse the variables.

chandresh_cool
  • 11,753
  • 3
  • 30
  • 45