Using Jquery with PHP
Using Jquery with PHP
我正在开发一个与用户谈判的程序。我想让用户引用价格,价格必须存储在一个javascript变量中,然后基于一些条件,程序将决定是否接受价格。
我已经在PHP中完成了程序,但我无法在javascript中编写此程序(我不希望每次用户引用价格时刷新页面,因此我必须在javascript中编写此程序),因为我是新手。
一旦用户按下接受价格按钮,我希望最终价格传递给PHP。
下面是我的PHP代码:
<?php
if (isset($_POST['accept'])) {
echo 'Price Accepted';
if (isset($_SESSION['my_quote'])) {
$_SESSION['cart_total']=$_SESSION['my_quote'];
}
unset($_SESSION['my_quote']);
//NOW PLACING THE ORDER
header("Location:checkout.php");
} else {
if (isset($_POST['user_quote']) && !empty($_POST['user_quote'])) {
$user_quote = sanitize($_POST['user_quote']);
if (!isset($_SESSION['my_quote'])) {
$_SESSION['my_quote']=$_SESSION['cart_total'];
}
if ($user_quote < 0.9 * $_SESSION['cart_total']) {
$_SESSION['my_quote'] = rand(0.9 * $_SESSION['cart_total'], $_SESSION['my_quote']);
echo 'Price Unacceptable. I can go upto : ' . $_SESSION['my_quote'];
}
} else {
$errors[]="Please Enter Something";
}
}
?>
<center>Your Current Total IS : Rs. <?php echo $_SESSION['cart_total'] ?></center>
<div class="alert-warning">
<a href="#" class="close" data-dismiss="alert" aria-label="alert">×</a>
<?php if(!empty($errors)){echo output_errors($errors);} ?>
</div>
<form class="negotiation_form" action="" method="POST" class="form">
<input type="text" name="user_quote" class="span4"><br>
<input type="submit" value="Quote !" class="btn ">
</form>
<form action="" method="POST" class="negotiation_form">
<input type="hidden" name="accept">
<input type="submit" value="Accept / Checkout" class="btn ">
</form>
您需要使用jquery并将请求异步发送到php文件,以防止页面重新加载。
下面是如何在客户端解决这个问题的基本脚本:<script src="/js/jquery.js" type="text/javascript"></script>
<script>
$(function() {
$('form').submit(function(event) {
$.ajax({ url: '/PHP/File/Path', data: { param1: $('#Id_of_Input_field').val() }, type: "POST" })
.success(function (result) {
// do something with result
});
// prevent the page from submitting the form
event.preventDefault();
});
});
</script>
希望这对你有帮助!
相关文章:
- timeago.js with datatable and PHP
- PHP $_GET with jQuery $.get call
- PHP Error with Javascript
- JavaScript with PHP SESSION
- Ajax Call with Post in PHP
- chartJs with MySQL and PHP
- HTML用PHP替换with函数
- InnerHTML with PHP
- Ajax call VERY SLOW with PHP
- jQuery load with php in it?
- Wordpress: POST to .php with Ajax
- Javascript array to PHP with json.stringify
- Call PHP with JavaScript
- js/php with include files
- Javascript array to php with jQuery ajax
- PHP with Javascript - What is the scope of PHP variables ins
- PHP with javascript
- Using TeeChart PHP with javascript
- GET id from php with javascript
- Javascript value to PHP with Jquery