有没有办法从html标签发送数据到PHP?

时间:2014-09-06 18:00:24

标签: javascript html

由于某些原因,我试图改变提交按钮的功能。如果调用了一个通过单击提交按钮调用的JS函数,但我不知道如何手动将我的数据从html标签发送到php变量。以下是代码的简短描述。

<html>
<body>
<input class="inputtext" id="email" name="email" type="text"></div>
<input value="Submit" name="v4l" id="login" class="inputsubmit" type="button" onclick="myFunction();return false">
<script>

function myFunction() {
  var TestVar =document.getElementById("email").value;
    document.write(TestVar);
//store data of testvar to php
}

</script>
<html>
<body>

我知道它可以通过表格完成,但我需要这样。

3 个答案:

答案 0 :(得分:0)

使用PHP表单将是一个非常简单的解决方案:http://www.w3schools.com/php/php_forms.asp 你几乎可以将它发布到PHP页面。

答案 1 :(得分:0)

我希望你对jQuery感到满意。

尝试

$("#login").click(function(){
        var email= $("#email").val();
        jQuery.post(
                    "*your parsing url*",
                    {email:email},
                    function(data){
                       // Data returned from the ajax call

                    }
                )
    });

答案 2 :(得分:0)

jQuery库使这个以及许多其他任务变得非常简单:

<html><head></head><body>

<form id="myForm">
  <input class="inputtext" id="email" name="email" type="text"></div>
  <input value="Submit" name="v4l" id="login" class="inputsubmit" type="button" onclick="myFunction();return false">
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function myFunction() {
  jQuery.ajax({
    url: 'yourcode.php',
    type: 'POST',
    data: jQuery('#myForm').serialize(),
    success: function(response) {
      alert('The data was sent, and the server said '+response);
    }
  });
}
</script>

</body></html>

有关详细信息,请参阅http://api.jquery.com/jquery.ajax/