Reputation: 115
I've been staring at this for far too long and just can't figure out what's wrong. I'm attempting to pull form data from a jquery form in javascript and then send it off to a php page which posts it to a mysql server. Here's my code:
"index2.html" (just the form page)
<title>Login Test</title>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Login</h1>
</div>
<div data-role='content'>
<form id="testform" action="/ajaxtest/userInfo2.php">
<label for="name" class="ui-hidden-accessible">Name:</label>
<input type="text" name="usr" placeholder="Username"/>
<label for="password" class="ui-hidden-accessible">Age :</label>
<input type="text" name="age" placeholder="Password"/>
<button id="sub">Submit</button>
</form>
</div>
</div>
</body>
Next is "my_script.js" which is properly included in "index2.html"
$("#sub").click( function() {
event.preventDefault();
$.post( "/ajaxtest/userInfo2.php",
$.(#testform).serializeArray()
);
});
And lastly "userInfo2.php" which is the php page that submits the data to my table
<?php
$conn = mysql_connect('localhost', 'root', 'root');
$db = mysql_select_db('myDatabase');
$name = $_POST['usr'];
mysql_query("INSERT INTO ajaxtest (name) VALUES ('$name')");
?>
It should be known that my table only has one field: "name". I was just trying to get one field to work and then continue expanding. Every time that I try and test this, it prints "undefined" on my userInfo2.php page and inserts a blank name into my table.
I tried throwing a GET line into my php to retrieve the same variable POST would, and that worked. It added the correct name to the table but still printed "undefined". However, I still want the POST to work...
I really hope it's just something small that I've overlooked, but thanks in advance for the help!
Upvotes: 0
Views: 538
Reputation: 33661
Your select is wrong
$.(#testform)
should be
$('#testform')
And you should be passing in the event argument
$("#sub").click( function(event) // <-- here
Upvotes: 2