2016-07-05 170 views
1

因此,我正在開發此項目,用戶可以註冊該新聞簡報,並將其名稱添加到在線數據庫中。我正在使用在線數據庫(CPanel - PhPMyAdmin)我一直堅持使用什麼類型的代碼來將這些數據從表單添加到數據庫中。將數據插入到cPanel數據庫

這是我到目前爲止,我不太確定如何從這裏走。任何幫助表示讚賞。

ALSO:我使用Bootstrap3和jQuery/AJAX

<div id="newsletter" class="container-fluid text-center"> 
      <h3>NEWSLETTER</h3> 

      <form id="signup-form" action="" method=""> 
       <div class="input-prepend" ><span class="add-on"><i class="glyphicon glyphicon-user"></i></span> 
       <label for="name">Your Full Name</label> 
       <input type="text" id="name" name="name" placeholder="your full name" size="40" style="color:black;" class="validate" required> 
       </div> 

       <div class="input-prepend"><span class="add-on"><i class="glyphicon glyphicon-envelope"></i></span> 
       <label for="email">Your Email</label> 
       <input type="email" id="email" name="email" placeholder="[email protected]" size="40" style="color:black;"class="validate" required> 
       </div> 

       <button id="sub" type="submit" class="btn btn-large"> Sign Up!</button> 
      </form> 
      <br/> 

</div> 

回答

1

您可以使用Ajax

示例 -

$('#button').click(function() {//give id of submit button 
    var val1 = $('#text1').val();//get value you need to post 
    var val2 = $('#text2').val();//get value you need to post 
    $.ajax({ 
     type: 'POST', 
     url: 'process.php', 
     data: { text1: val1, text2: val2 }, 
     success: function(response) { 
      $('#result').html(response);//responce you recevied 
     } 
    }); }); 

使用Post方法獲取這兩個值。執行您的操作並在結果中發送真實的錯誤。

---------------地名釋義與新的例子----------------

HTML文件深:ajaxsubmit.html

<!DOCTYPE html> 
<html> 
<head> 
<title>Submit Form Using AJAX and jQuery</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<link href="css/refreshform.css" rel="stylesheet"> 
<script src="script.js"></script> 
</head> 
<body> 
<div id="mainform"> 
<h2>Submit Form Using AJAX and jQuery</h2> <!-- Required div Starts Here --> 
<div id="form"> 
<h3>Fill Your Information !</h3> 
<div> 
<label>Name :</label> 
<input id="name" type="text"> 
<label>Email :</label> 
<input id="email" type="text"> 
<label>Password :</label> 
<input id="password" type="password"> 
<label>Contact No :</label> 
<input id="contact" type="text"> 
<input id="submit" type="button" value="Submit"> 
</div> 
</div> 
</div> 
</body> 
</html> 

PHP文件:ajaxsubmit.php

<?php 
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server.. 
$db = mysql_select_db("mydba", $connection); // Selecting Database 
//Fetching Values from URL 
$name2=$_POST['name1']; 
$email2=$_POST['email1']; 
$password2=$_POST['password1']; 
$contact2=$_POST['contact1']; 
//Insert query 
$query = mysql_query("insert into form_element(name, email, password, contact) values ('$name2', '$email2', '$password2','$contact2')"); 
echo "Form Submitted Succesfully"; 
mysql_close($connection); // Connection Closed 
?> 

jQuery的文件:的script.js

​​

MY-SQL代碼段:

CREATE DATABASE mydba; 
CREATE TABLE form_element(
id int(10) NOT NULL AUTO_INCREMENT, 
name varchar(255) NOT NULL, 
email varchar(255) NOT NULL, 
password varchar(255) NOT NULL, 
contact varchar(255) NOT NULL, 
PRIMARY KEY (id) 
) 
+0

什麼是對process.php? – tash517

+0

我沒有完全瞭解你的process.php?我正在更新我的答案,以根據你的問題指導寫入的內容 –

+0

哦,我真的沒有任何PHP文件atm,所以我很困惑它是什麼 – tash517