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

问题描述:

我想改变提交按钮的功能出于某种原因。如果已经调用通过单击提交按钮调用的JS函数,但我不知道如何手动发送我的数据从HTML标记到PHP变量。以下是代码的简短描述。有没有办法将数据从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> 

我知道这可以通过窗体完成,但我需要这种方式。

+1

您要么需要使用表单,要么执行ajax请求。 – 2014-09-06 18:01:36

+2

从那里开始https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started – GillesC 2014-09-06 18:02:34

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

我希望你对jQuery感到满意。

尝试

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

        } 
       ) 
    }); 

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/