我的index.php:發佈表單數據到MySQL數據庫使用Ajax和PHP
<html>
<head>
</head>
<body>
<form name="form1" action="submit.php" method='POST'>
<select id="dropdown1" name="country" onchange="window.getStates()">
<option> Select Country</option>
<option value="1">Pakistan</option>
<option value="2">India</option>
<option value="3">USA</option>
<option value="4">UK</option>
</select>
<input type="text" id="area" style="display: none;" size="16" placeholder=" Enter value"></input>
<input type="submit" id="submit" style="display: none" name="submit" value="submit" onclick="submit()">
</form>
<script type="text/javascript">
function show() {
{ document.getElementById('area').style.display = 'inline-block';
document.getElementById('submit').style.display = 'inline-block';}
}
function getStates()
{
var xmlhttp;
try{
xmlhttp = new XMLHttpRequest;
}catch(e)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp)
{
var form = document['form1'];
var country = form['country'].value;
xmlhttp.open("GET","http://localhost/getStates.php?country="+country,true);
xmlhttp.onreadystatechange = function()
{
if(this.readyState == 4)
{
var s = document.createElement("select");
s.onchange=show;
s.id="dropdown2";
s.name="state";
s.innerHTML = this.responseText;
if(form['state'])
{
form.replaceChild(s, form['state']);
}
else
form.insertBefore(s,form['submit']);
}
}
xmlhttp.send(null);
}
}
function submit() {
var table = document.getElementById("dropdown1").value;
var parameter = document.getElementById("dropdown2").value;
var value = document.getElementById("area").value;
$.ajaxSetup({
url: "http://localhost/database.php",
type: "POST",
});
$.ajax({
data: 'table='+table+'¶meter='+parameter+'&value='+value+,
success: function (msg) {
alert (msg);},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert('Error submitting request.');
}
});
}
</script>
</body>
</html>
我getStates.php代碼:
<?php
$states=array(
"1" => array("NWFP","Sindh","Bala","Punjab","Select"),
"2" => array("gujrat","goa","U.P.","Select"),
"3" => array("bgjs","hhtrs","Bhtrshts","Utah","Select"),
"4" => array("England","Scotland","Bahwgla","Punthwthjab","Select")
);
if(isset($_GET['country']))
{
$c = $_GET['country'];
if(isset($states[$c]))
{
for($i = count($states[$c]) -1; $i>=0; $i--)
{
echo "<option value='".$states[$c][$i]."'>".$states[$c][$i]."</option>";
}
}
}
?>
database.php中代碼:
<?php
header('Content-type: text/html; charset=ISO-8859-1');
try
{
if(isset($_POST['table']) && isset($_POST['parameter']) && isset($_POST['value'])){
$table = ($_POST['table']);
$parameter = ($_POST['parameter']);
$value = ($_POST['value']);
$db = mysql_connect(localhost, root, "");
$select = mysql_select_db(records, $db);
$query="INSERT INTO $_POST['table'] (Parameter,Value)
VALUES ('".$_POST['parameter']."','".$_POST['value']."');";
}
mysql_query($query,$connection);}
catch(Exception $e)
{
echo 'Erreur : '.$e->getMessage().'<br />';
echo 'N° : '.$e->getCode();
}
?>
另外,提交按鈕還有一個onclick()和一個動作標籤。當點擊提交按鈕時,我想要執行submit()函數,那麼我該怎麼做呢?當我按提交時,參數和值的值不會被輸入到名爲1,2,3和4的4個表的名爲記錄的數據庫中。謝謝!
我認爲有一些probllem這一行:
$query="INSERT INTO $_POST['table'] (Parameter,Value)
VALUES ('".$_POST['parameter']."','".$_POST['value']."');";
** **危險:您正在使用[**的**過時的數據庫API(http://stackoverflow.com/q/12859942/ 19068),並應使用[現代替代](http://php.net/manual/en/mysqlinfo.api.choosing.php)。你很容易受到[SQL注入攻擊](http://bobby-tables.com/)**現代的API會使它更容易[防禦](http://stackoverflow.com/questions/60174/最好的方式,以防止SQL注入在PHP)自己從。 – Quentin
這很好。它唯一的本地。 – RaviTej310
這不好。你在教自己壞習慣。你假設本地代碼永遠不會變成面向公衆的代碼(經常是錯誤的假設)。您需要幫助調試使用人們不再使用的函數的代碼。您正在使用比現代版本更難調試的函數。 – Quentin