2016-12-16 61 views
1

我想使用ajax將表單中的數據插入數據庫。當我運行它時,它只顯示index.php代碼,什麼都不做。我無法找出錯誤。所以,請幫助我運行此代碼。使用ajax插入MySql數據庫

的index.php

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 

</head> 

<body> 
<input type="text" id="name" placeholder="Enter Text"> 
<button id="submit" onClick="js()" type="button"> Submit</button> 

<script> 
function js() { 

var name = document.getElementById("name").value ; 


$.ajax({ 

type:'POST', 
data: name, 
url:"insert.php", 
success:function(result){ 
alert(success); 
} 

}); 
} 

</script> 
</body> 



</html> 

insert.php

<?php 
$connection = mysqli_connect('localhost', 'root', '', 'sample'); 
if($_POST['name']){ 
$name=$_POST['name']; 
$q= "insert into test ('$name')"; 
$query = mysqli_query($connection, $q); 
if($query){ 
echo 'inserted'; 
} 
} 
?> 
+0

什麼是'test'的表結構?如果多於1列,則需要指定列名稱。你也可以執行'$ query = mysqli_query($ connection,$ q)或者死(mysqli_error($ connection));'調試 – Sean

+1

你需要在'index.php'文件中包含'jquery'。一種資源可以是[this](https://developers.google.com/speed/libraries/#jquery) –

回答

0

如果下面給出的表結構:

id int PK (Auto_increment) 
name varchar(30) not null 

你必須使用下面的代碼:

index.php

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> 
<title>Test</title> 
</head> 
<body> 
<input type="text" id="name" placeholder="Enter Text"> 
<button id="submit" onClick="js()" type="button"> Submit</button> 
<script> 
function js() 
{ 
    if(document.getElementById("name").value!="") 
    { 
     var name = document.getElementById("name").value ; 
     $.post("insert.php",{name:name},function(data){ 
      if(data) 
      { 
       alert(data); 
      } 
      else 
      { 
       alert("Cancel."); 
      } 
     }); 
    } 
    else 
    { 
     alert("Enter name."); 
     document.getElementById("name").focus(); 
    } 
} 
</script> 
</body> 
</html> 

insert.php

<?php 
    $connection = mysqli_connect('localhost', 'root', '', 'test'); 
    if($_POST['name']) 
    { 
     $name=$_POST['name']; 
     $q= "insert into test(name) values ('$name')"; 
     $query = mysqli_query($connection, $q); 
     if($query) 
     { 
      echo 'inserted'; 
     } 
     else 
     { 
      echo 'no inserted'; 
     } 
    } 
?> 

如果您有任何困惑,請讓我知道。

謝謝...

0

嘗試以下...這可能對你有所幫助..

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> 
</head> 

<body> 
<input type="text" id="name" placeholder="Enter Text"> 
<button id="submit" onClick="js()" type="button"> Submit</button> 

<script> 
function js() { 

var name = $("#name").val(); 

$.ajax({ 

type:'POST', 
data: 'name='+name, 
url:"insert.php", 
success:function(result){ 
alert(success); 
} 

}); 
} 

</script> 
</body> 
</html> 

和PHP腳本如下所示。

<?php 
$connection = mysqli_connect('localhost', 'root', '', 'sample'); 
if($_POST['name']){ 
$name=$_POST['name']; 
$q= "INSERT INTO test (name) VALUES ('$name')"; 
$query = mysqli_query($connection, $q); 
if($query){ 
echo 'inserted'; 
} 
} 
?>