如何在不提交表单或刷新页面的情况下将 jquery 中变量的值传递给 PHP



我的ajax是这样的:

$.ajax({
    type: "POST",
    url: 'index.php',
    datatype: 'html',
    data: { password : password}
});

一直试图将其存储在$pword.但它不存储。.PHP:

if(isset($_POST['password']))
{
    // store session data
    $pword=$_POST['password'];
}

HTML是这样的:

<input type="password" placeholder="password" id="password" name="password"/>

请帮忙。

首先:服务器端(PHP)和HTML代码没有错。

所以对于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

如果您解释一下您要做什么,我可以留下更详细的帮助。

<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>

使用 form.serialize() 发送数据。

$.ajax({
            type: "POST",
            url: 'index.php',
            datatype: 'html',
            data: form.serialize()
});

HTML

<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" onclick="sendData()" value="Ok">

爪哇语

function sendData(){
   
    $.ajax({
        type: "POST",
        url: 'index.php',
        datatype: 'html',
        data: { password : $("#password").val()}
    }); 
}

您可以使用javascript setTimeout函数将表单值传递到php页面中,如下所述,

我在 setTimeout 函数中添加了一个名为 passData() 的函数,因此它调用了提到的 passData() 函数延迟。

.HTML:

<input type="password" placeholder="password" id="password" name="password"/>

Javascript:

setTimeout(function() { passData() }, 3000);
function passData(){
   var password = $("#password").val(); 
    $.ajax({
        type: "POST",
        url: 'index.php',
        datatype: 'html',
        data: { password : password}
    }); 
}

从jquery文档中,$.ajax请求应如下所示:

$.ajax({
  method: "POST",
  url: "some.php",
  dataType: 'json',
  data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
  alert( "Data Saved: " + msg );
});

另一个建议是创建一个按钮。按下它时,数据将被发送到PHP文件。

<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" id="submit" name="submit" value="submit" />

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 );
        });
    });
});

相关内容

最新更新