[英]How do I pass value of a variable in jquery to PHP without submitting form or refreshing the page?
My ajax is this: 我的ajax是这样的:
$.ajax({
type: "POST",
url: 'index.php',
datatype: 'html',
data: { password : password}
});
Have been trying to store it in $pword
. 一直试图将其存储在
$pword
。 But it doesn't store. 但它不存储。 PHP:
PHP:
if(isset($_POST['password']))
{
// store session data
$pword=$_POST['password'];
}
HTML is this: HTML是这样的:
<input type="password" placeholder="password" id="password" name="password"/>
Please help. 请帮忙。
first of all : there is nothing wrong with server side (PHP) and HTML code. 首先: 服务器端(PHP)和HTML代码没有错。
so for jQuery portion : you need to correct a line like i did below: 因此对于jQuery部分:您需要像我在下面那样纠正一行:
$.ajax({ type: "POST", url: 'index.php', datatype: 'html', data: { password : $("#password").val() //here i asked jquery to fetch entire value of my html input(with an id of "password") as text(Not an Object) so it gives me the entered text value. }}) .success(function(data) { alert(data.toString()); });//optional function:runs when server responses to your request
i can leave more detailed help if you explain what exactly you r going to do. 如果您解释您将要做什么,我可以留下更详细的帮助。
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#password').on('blur',function(){
var password = $('#password').val();
$.ajax({
type: "POST",
url: 'index.php',
datatype: 'html',
data: { password : password
},
success: function(data){
alert(data);
}
});
})
})
</script>
<form method="post">
<input type="password" placeholder="password" id="password" name="password"/>
</form>
use form.serialize() to send data. 使用form.serialize()发送数据。
$.ajax({
type: "POST",
url: 'index.php',
datatype: 'html',
data: form.serialize()
});
From jquery documentation the $.ajax request should look like this: 从jquery文档中, $ .ajax请求应如下所示:
$.ajax({
method: "POST",
url: "some.php",
dataType: 'json',
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
Another suggestion would be to create a button. 另一个建议是创建一个按钮。 When pressing it the data will be sent to the PHP file.
按下时,数据将发送到PHP文件。
<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" id="submit" name="submit" value="submit" />
The jQuery code would be: jQuery代码为:
$(document).ready(function(){
$('#submit').click(function(){
$.ajax({
method: "POST",
url: "my-php-file.php",
dataType: 'json',
data: { password: $('#password').val() }
})
.done(function( msg ) {
alert( "Data sent: " + msg );
});
});
});
HTML HTML
<input type="password" placeholder="password" id="password" name="password"/> <input type="button" onclick="sendData()" value="Ok">
Javascript 使用Javascript
function sendData(){ $.ajax({ type: "POST", url: 'index.php', datatype: 'html', data: { password : $("#password").val()} }); }
You can use javascript setTimeout function to pass the form value into php page as mentioned below, 您可以使用javascript setTimeout函数将表单值传递到php页面,如下所述,
I have added a function called passData() inside the setTimeout function, so it calls the passData() function mentioned delay. 我在setTimeout函数内部添加了一个名为passData()的函数,因此它调用了提到的delay的passData()函数。
HTML: HTML:
<input type="password" placeholder="password" id="password" name="password"/>
Javascript: 使用Javascript:
setTimeout(function() { passData() }, 3000);
function passData(){
var password = $("#password").val();
$.ajax({
type: "POST",
url: 'index.php',
datatype: 'html',
data: { password : password}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.