0

I have the following code that should add a new item to the database table but it doesnt seem to be adding?

<html>
<head>
<title>Using Default Checkbox Values</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js">
</script>
<script type="text/javascript" >
$(function() {
$(".submit").click(function() {
var name = $("#name").val();
var username = $("#username").val();
var password = $("#password").val();
var gender = $("#gender").val();
var dataString = 'name='+ name + '&username=' + username + '&password=' + password + '&gender=' + gender;

if(name=='' || username=='' || password=='' || gender=='')
{
$('.success').fadeOut(2).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "join.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
</script>

</head>
<body>

<form method="post" name="form">
<ul><li>
<input id="name" name="name" type="text" />
</li><li>
<input id="username" name="username" type="text" />
</li><li>
<input id="password" name="password" type="password" />
</li><li>
<select id="gender" name="gender"> 
<option value="">Gender</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</li></ul>
<div >
<input type="submit" value="Submit" class="submit"/>
<span class="error" style="display:none"> Please Enter Valid Data</span>
<span class="success" style="display:none"> Registration Successfully</span>
</div></form&gt;

</body>
</html>

and it should run this:

<?php
include('db_login.php');
if($_POST)
{
$name=$_POST['name'];
$username=$_POST['username'];
$password=$_POST['password'];
$gender=$_POST['gender'];

$query1 = "INSERT INTO `unknown`.`comments` (`id`, `user`, `comment`, `date`, `other`) VALUES (NULL, '$name, '$username', '2010-10-23', '$password')";
$result1 = mysql_query( $query1 );
mysql_close($connection);
}else { }

?>
2
Contributors
1
Reply
5
Views
6 Years
Discussion Span
Last Post by hielo
1

the problem is due to a missing apostrophe after $name : (NULL, '$name[B]'[/B], '$username', '2010-10-23', '$password') But I suggest you try the following:

<html>
<head>
<title>Using Default Checkbox Values</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" >
$(function() {
	$(".submit").click(function() {
		var name = encodeURIComponent($.trim($("#name").val()));
		var username = encodeURIComponent($.trim($("#username").val()));
		var password = encodeURIComponent($.trim($("#password").val()));
		var gender = encodeURIComponent($.trim($("#gender").val()));
		var dataString = 'name='+ name + '&username=' + username + '&password=' + password + '&gender=' + gender;

		if(name=='' || username=='' || password=='' || gender=='')
		{
			$('.success').fadeOut(2).hide();
			$('.error').fadeOut(200).show();
		}
		else
		{
			$.ajax({
				type: "POST",
				url: "join.php",
				data: dataString,
				dataType:'json',
				success: function(data)
					{
						if(!data.error)
						{
							$('.success').fadeIn(200).show();
							$('.error').fadeOut(200).hide();
						}
						else
						{
							alert(data.error);
						}
					}
				});
		}
		return false;
	});
});
</script>

</head>
<body>

<form method="post" name="form" action="join.php">
<ul><li>
<input id="name" name="name" type="text" />
</li><li>
<input id="username" name="username" type="text" />
</li><li>
<input id="password" name="password" type="password" />
</li><li>
<select id="gender" name="gender"> 
<option value="">Gender</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</li></ul>
<div >
<input type="submit" value="Submit" class="submit"/>
<span class="error" style="display:none"> Please Enter Valid Data</span>
<span class="success" style="display:none"> Registration Successfully</span>
</div></form>

</body>
</html>


<?php
include('db_login.php');
if( isset($_POST) && !empty($_POST) )
{
	$status='{"status":%d,"error":"%s", "id":%d}';
	$name=$_POST['name'];
	$username=$_POST['username'];
	$password=$_POST['password'];
	$gender=$_POST['gender'];

	$query1 = "INSERT INTO `unknown`.`comments` (`id`, `user`, `comment`, `date`, `other`) VALUES (NULL, '$name', '$username', '2010-10-23', '$password')";
	$result1 = mysql_query( $query1 ) or die( sprintf($status, -1, mysql_error(), -1) );
	echo sprintf($status, 0, '',mysql_insert_id());
}
else
{
	echo sprintf($status, -2,'Nothing was posted',-1);
}
mysql_close($connection);
exit;
?>

Edited by hielo: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.