向mysql数据库PHP和AJAX添加表单数据

时间:2022-02-08 06:56:48

I am wanting to have a form that will let me add names to my database without refreshing the form. I have been working on it but it does not seem to work. I am quite new at this so any help is appreciated.

我希望有一个表单,可以在不刷新表单的情况下将名称添加到数据库中。我一直在做这件事,但似乎没有效果。我对这方面很陌生,所以我很感激你的帮助。

For my index.html I have:

我的指数。html我有:

 <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 = $("#firstname").val();
var username = $("#lastname").val();

var dataString = 'firstname='+ firstname + 'lastname=' + lastname

if(firstname=='' || lastname=='')
{
$('.success').fadeOut(200).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>



<body>

<form method="post" name="form">
<ul><li>
<input id="firstname" name="firstname" type="text" />
</li><li>
<input id="lastname" name="lastname" type="text" />


</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> 

For join.php:

join.php:

<?php
include("db.php");

if($_POST)
{
$firstname=$_POST['firstname'];
$lastname=$_POST['username'];
mysql_query("INSERT INTO persons (firstname,lastname) VALUES('$firstname','$lastname')");
}

?>

and db.php:

和db.php:

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "test";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>

1 个解决方案

#1


3  

there is mistake in code, you have coded

代码中有错误,你已经编码了

  var name = $("#firstname").val();
  var username = $("#lastname").val();
  var dataString = 'firstname='+ firstname + 'lastname=' + lastname

you have to used the variables name and username in dataString but you have written the id(s) of fields for first and last name. change either the variable name or variables used in dataString

您必须在dataString中使用变量名和用户名,但是您已经为名和姓编写了字段的id。更改dataString中使用的变量名或变量

corrected line is :

纠正是行:

 var dataString = 'firstname='+ name + 'lastname=' + username

And

   $lastname=$_POST['username']; =>  $lastname=$_POST['lastname']; 

#1


3  

there is mistake in code, you have coded

代码中有错误,你已经编码了

  var name = $("#firstname").val();
  var username = $("#lastname").val();
  var dataString = 'firstname='+ firstname + 'lastname=' + lastname

you have to used the variables name and username in dataString but you have written the id(s) of fields for first and last name. change either the variable name or variables used in dataString

您必须在dataString中使用变量名和用户名,但是您已经为名和姓编写了字段的id。更改dataString中使用的变量名或变量

corrected line is :

纠正是行:

 var dataString = 'firstname='+ name + 'lastname=' + username

And

   $lastname=$_POST['username']; =>  $lastname=$_POST['lastname'];