0

I'm experiencing some kind of a problem here, I have no idea what's wrong with this code. I'm pretty sure it's client sided since I am not getting any PHP errors, but I may be wrong.

I'm trying to get a form, once submitted, to insert the information contained in a text field into a MySQL database via an AJAX request to a PHP file.

Here's where I'm at:

/* index.php */

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="scripts/ajax.js"></script>


//...


        <div class="success" id="success"></div>
        <div class="err" id="err"></div>


 <!--Create Form-->
 <form action="" method="post" name="create" id="createForm" onsubmit="createNew(document.create.create2.value); return false;">        
        <h5>Create New File</h5>
        <p><input name="create2" type="text" maxlength="32" /></p>
 <input type="submit" name="submit" value="Create" />
 </form>


/* ajax.js */

function createNew(name)
{
 $('#loading').css('visibility','visible');

  $.ajax({
  type: "POST",
  url: "../utilities/process.php",
  data: 'name='+name,
  datatype: "html",
  success: function(msg){

   if(parseInt(msg)!=5)
   {
    $('#success').html('Successfully added ' + name + ' into database.');
    $('#loading').css('visibility','hidden');
    alert('success');//testing purposes
   }
   else
   {
    $('#err').html('Failed to add ' + name + ' into database.');
    $('#loading').css('visibility','hidden');
    alert('fail');//testing purposes
   }
  }
      })
}


/* process.php */

<?
include('db_connect.php');

if($_POST['name'])
{
 $name = $_POST['name'];
 $name = mysql_escape_string($name);
 $query = "INSERT INTO tz_navbar (name) VALUES (".$name.")";
 $result = mysql_query("$query") or die ("5");

}
?>

Here's my problem: After I submit my form with something, it reports that is succeeds but nothing gets added to my database.

Thank you all in advance for taking your time to help me.

3
  • You can always try $result = mysql_query($query) or die(mysql_error()); to capture the mysql error message. This will tell you exactly what's going on with any failed query. Commented Jan 25, 2011 at 19:45
  • On a security note, make sure you escape the value of $name before storing it in your database. A malicious user could take advantage of this query as you currently have it written. For more information, see the Bobby Tables entry on PHP: bobby-tables.com/php.html Commented Jan 25, 2011 at 20:44
  • As you can see, calvinf. [$name = mysql_escape_string($name);] This part escapes the strings. Commented Jan 26, 2011 at 4:20

3 Answers 3

4

Looking at your query, I suspect you need it to be:

$query = "INSERT INTO tz_navbar (name) VALUES ('".$name."')";

If that doesn't fix it, you need to log the value of $_REQUEST

error_log(print_r($_REQUEST,true));

to ensure that you are getting the right values on the server side.

Sign up to request clarification or add additional context in comments.

2 Comments

I think Paul is right. I suspect that your post value isn't getting passed to your PHP script properly. Trying using the JSON style to pass your values in your AJAX request. { name: name }
It seems to be passed perfectly, it was really an attention error from my part on the query.
1

You have $_POST['name'] , but in your form you have, input type = text name = "create" Which should be $_POST['create'] . That is why your $_POST['name'] does not have any value when it's passed.

Comments

0

Check your query.

$query = "INSERT INTO tz_navbar (name) VALUES ('$name');

Also, you could try testing process.php without javascript. Just so you could see the mysql error message. Use die() or something.

1 Comment

In fact, I used die(mysql_error()); and I used alert(); with javascript to get my error from process.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.