[英]Pass form values from PHP file to another PHP file
我在home.php中制作了一個這樣的彈出表單:
<script src="js/submit.js"></script>
.........
.........
.........
<div id="abc">
<!-- Popup Div Starts Here -->
<div id="popupContact">
<!-- Form -->
<form action="#" id="form" method="post" name="form">
<input id="month" name="month" placeholder="MONTH" type="text">
<a href="javascript:%20check_empty()" id="submit">ADD</a>
</form>
</div>
<!-- Popup Div Ends Here -->
</div>
我填寫表格。 當我單擊“添加”按鈕時,它將運行javascript函數。 Submit.js中的代碼:
function check_empty() {
if (document.getElementById('month').value == ""){
alert("Fill column!");
} else {
document.getElementById('form').submit();
$.get("application/insertdata.php");
return false;
}
}
//Function To Display Popup
function div_show() {
document.getElementById('abc').style.display = "block";
}
//Function to Hide Popup
function div_hide(){
document.getElementById('abc').style.display = "none";
}
我想在insertdata.php中運行查詢,如下所示。 它需要“月”中的值。
<?php
require("phpsqlajax_dbinfo.php");
$conn = mysqli_connect('localhost', $username, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$data = isset($_POST['month']);
$monthstring = mysqli_real_escape_string($conn, $data);
$sql = "INSERT INTO `databasea`.`tablea` (`MONTH`, `TEST`) VALUES ('". $monthstring ."', 'xxx');";
mysqli_query($conn, $sql);
mysqli_close($conn);
?>
查詢成功運行,並且行已添加到我的表中。 “測試”列中添加了“ xxx”。 但是在“ MONTH”列中,它不產生任何值,只是空的。
那么,如何獲得“月”值呢? 謝謝。
由於您使用的是JavaScript / jQuery,因此在HTML中實際上不需要內聯代碼,因此讓我們從刪除內聯JavaScript開始:
<script src="js/submit.js"></script>
.........
.........
.........
<form action="#" id="form" method="post" name="form">
<input id="month" name="month" placeholder="MONTH" type="text">
<a href="#" id="submit">ADD</a>
</form>
清潔得多,不是嗎? 您沒有在函數調用中傳遞任何可能給您帶來麻煩的數據。
現在,在JavaScript / jQuery中進行一個更簡單的設置,在該設置中,我們將捕獲click事件並通過$.post
傳遞數據:
$('#submit').click(function(event) {
event.preventDefault(); // prevent the default click action
var month = $('#month').val();
if('' == month) {
alert('fill the column!');
} else {
$.post("application/insertdata.php", {month: month}); // notice how the data is passed
}
});
到目前為止,到目前為止,代碼更加緊密,可讀性強,並且實際上將數據從表單發布到AJAX調用中。
最后是PHP,測試以查看變量month
是否設置正確:
<?php
require("phpsqlajax_dbinfo.php");
$conn = mysqli_connect('localhost', $username, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['month'])) {
$data = $_POST['month'];
$monthstring = mysqli_real_escape_string($conn, $data);
$sql = "INSERT INTO `databasea`.`tablea` (`MONTH`, `TEST`) VALUES ('". $monthstring ."', 'xxx');";
mysqli_query($conn, $sql);
}
mysqli_close($conn);
?>
注意 :我擔心您的頁面上可能有多個以上表格,並且您可能正在重復使用ID,但這些ID無法使用,並且需要刪除重復的ID。 如果是這種情況,則需要更改我編寫的jQuery代碼。 這是一種方法:
$('a').click(function(event) {
event.preventDefault(); // prevent the default click action
var month = $(this).prev('input').val(); // get the input next to the link
if('' == month) {
alert('fill the column!');
} else {
$.post("application/insertdata.php", {month: month});
}
});
正如我在評論中所述, Little Bobby說您的腳本有遭受SQL注入攻擊的危險。 了解有關MySQLi的 准備好的語句。 即使轉義字符串也不安全! 更改為准備好的語句將使您的代碼更干凈,更安全。
嗨,使用$data = $_POST['month'];
isset
將返回true
或false
而不是month的值
更換
$data = isset($_POST['month']);
通過
if(isset($_POST['month'])) {
$data=$_POST['month'];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.