简体   繁体   English

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

[英]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.

相关问题 提交表单而不刷新页面 - Submitting a form without refreshing the page 需要将变量从javascript传递到php,而无需刷新或提交 - need to pass variable from javascript to php without refreshing or submitting 如何在页面加载时在Javascript变量和php值之间制作php IF语句而不提交表单 - How to make php IF statement between Javascript variable and php value on page load without submitting form 如何将动态ID文本框值传递给另一个页面而不用jquery和php刷新 - How to pass dynamic id text box value to another page without refreshing with jquery and php 如何在不刷新页面的情况下不断更新PHP变量? - How can I continuously update a PHP variable without refreshing a page? 如何在不刷新页面的情况下自动提交此表单? - How do I automatically submit this form without refreshing the page? 如何提交表单而不刷新当前页面? - How do I submit a form without refreshing the current page? 如何在不提交PHP表单的情况下检索DropDownBox的值? - How do I retrieve the value of a DropDownBox without submitting its form in PHP? 在php中提交表单后如何加载上一页? - How do I load the previous page after submitting a form in php? 将值从 html 表单传递给 php 而不提交表单 - Pass value from html form to php without submitting the form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM