0

i have this code, and while testing two values to insert into mysql database, it just doesnt insert anything. Am I mixing up values or is there an error in my code? Using a wamp server. The following errors that occur;

Undefined index: name in C:\wamp\www\info.php on line 3  
Undefined index: desc in C:\wamp\www\info.php on line 4  

But i tried playing with the values and couldn't figure it out.

index.html

   <html>
<head><title></title></head>
<body>

<form id="myForm" action="userInfo.php" method="post">
Name: <input type="text" name="name" /><br />
Age : <input type="text" name="age" /><br />
<button id="sub">Save</button>
</form>

<span id="result"></span>

<script src="jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="my_script.js" type="text/javascript"></script>
</body>
</html>

userinfo.php

    <?php
         include_once('db.php');

        $name = $_POST['name'];
        $age = $_POST['age'];                  
        if(mysql_query("INSERT INTO user VALUES('$name', '$age')"))
          echo "Successfully Inserted";
        else
          echo "Insertion Failed";
?>

myscript part

$("#sub").click( function() {
 $.post( $("#myForm").attr("action"),
         $("#myForm :input").serializeArray(),
         function(info){ $("#result").html(info);
  });
});
$("#myForm").submit( function() {
  return false;
});

function clearInput() {
    $("#myForm :input").each( function() {
       $(this).val('');
    });
}  

Update: still no fix. Getting the error:

 Notice: Undefined index: name in C:\wamp\www\userInfo.php on line 4

Notice: Undefined index: age in C:\wamp\www\userInfo.php on line 5
Insertion Failed
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) that has been [removed](http://php.net/manual/en/mysql.php) from PHP. You should select a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **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 Dec 02 '16 at 11:18
  • 1. Don't mix up single quote & double quote in HTML codes (for tidiness sake); 2. Don't use deprecated `mysql_*` functions (use MySQLi / PDO instead); 3. Watch out for SQL injection attacks. – Raptor Dec 02 '16 at 11:18

1 Answers1

-1

Can you share what javascript code you're using? Without seeing the ajax call it's difficult to help.

My two cents would be use firebug or the native browser network traffic watcher to check the headers send to the server. You want to be sure that when you run your ajax request you pass in all post parameters.

Also make sure your ajax request is of type POST and you're filling the data field of your $.post call, like:

$.post('url_here', { "name": "form_name_value here", "desc" : "desc_for_value_here" })

UPDATE For serializeArray target only the form element: https://api.jquery.com/serializeArray/

Like:

$("#myForm").serializeArray()
Hangarter
  • 582
  • 4
  • 12