我的代码应该存储用户插入的信息并将其存储在数据库中。
应该在单击按钮时调用此函数,但问题是该函数在页面加载和按下按钮时运行。
这是我的代码
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
<?php
function write_command($name,$address,$telephone,$cart){
$servername = 'localhost';
$server_username = 'root';
$server_password = '';
$DB_name = 'OnlineGarcon';
$DB_connect = mysqli_connect($servername,$server_username,$server_password,$DB_name);
if ($DB_connect -> connect_error){
echo 'unable to connect to due to server error!';
}else{
$_write_command = "INSERT INTO `orders`(`name`, `address`, `telephone`, `cart`) VALUES ('$name','$address','$telephone','$cart')";
if ($DB_connect->query($_write_command) == TRUE) {
echo "Order was placed,Thanks!\nyour order was:$cart";
} else {
echo "Server error!";
}
}
}
?>
<center>
<form method="POST">
<input name="name" class="field" placeholder="Full name"></input>
<br>
<br>
<input name="address" class="field" placeholder="Address"></input>
</br>
</br>
<input name="telephone" class="field" placeholder="Telephone/Mobile number"></input>
<br>
<br>
<br>
<br>
<button id="submit">Submit order</button>
</br>
</br>
</br>
</br>
</form>
<script>
$("#submit").click(function(){
alert('<?php
$name = $_POST['name'];
$address = $_POST['address'];
$telephone = $_POST['telephone'];
$cart = $_GET['cart'];
write_command($name,$address,$telephone,$cart);
?>');
});
</script>
</body>
</html>
答案 0 :(得分:0)
使用jQuery Ajax调用,您可以非常简单地完成此操作。
form.php的
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
<input name="name" id="name" placeholder="Full name">
<input name="address" name="address" placeholder="Address">
<input name="telephone" id="telephone" placeholder="Telephone/Mobile number">
<button id="submit">Submit order</button>
<script type="text/javascript">
$('#submit').click( function() {
$.ajax({
url: "functions.php",
type: "POST",
data: {
name: $('#name').val();
address: $('#address').val();
telephone: $('#telephone').val();
cart: <?php echo $_GET['cart']; ?>
}
});
});
</script>
</body>
</html>
的functions.php
<?php
function write_command($name, $address, $telephone, $cart) {
$servername = 'localhost';
$server_username = 'root';
$server_password = '';
$DB_name = 'OnlineGarcon';
$DB_connect = mysqli_connect($servername,$server_username,$server_password,$DB_name);
if( $DB_connect->connect_error ){
echo 'unable to connect to due to server error!';
} else {
$_write_command = "INSERT INTO `orders`(`name`, `address`, `telephone`, `cart`) VALUES ('$name','$address','$telephone','$cart')";
if ($DB_connect->query($_write_command) == TRUE) {
echo "Order was placed,Thanks!\nyour order was:$cart";
} else {
echo "Server error!";
}
}
}
write_command( $_POST['name'], $_POST['address'], $_POST['telephone'], $_POST['cart'] );
答案 1 :(得分:-1)
这将会发生,您需要做的是
<form method="POST" onsubmit="callFuntion()">
<form action="demo_form.php" method="post">
将所有的PHP代码放在文件demo_form.php中,当点击提交时,它将转到该文件,这将帮助您调用 功能一次不是两次。