我如何将js数据传递给php?

时间:2014-12-02 13:30:10

标签: javascript php html5

<html>

<head>
    <script type="text/javascript">
function tally()
{
    var scrt_var = 10;


    var Dpoint, Ipoint, Hpoint, Apoint;
    var party_Score = ['Dpoint', 'Ipoint', 'Hpoint', 'Apoint'];
    var D, I, H, A;
    var value_Point;
    var type_Choice;
    var tag_Choice;


    var inputs = document.getElementsByTagName("input"),
        iLength = inputs.length,
    D = I = H = A = 0;

    for (i = 0; i < iLength; i++) if (inputs[i].checked)
    {
        value_Point = parseInt(inputs[i].value);
        if (inputs[i].name.search('D') > -1){ D += value_Point; }
        if (inputs[i].name.search('I') > -1){ I += value_Point; }
        if (inputs[i].name.search('H') > -1){ H += value_Point; }
        if (inputs[i].name.search('A') > -1){ A += value_Point; }
    }   //check here !!!~
Dpoint = D; // converting this to php data so i can send it by email
Ipoint = I; // converting this to php data so i can send it by email
Hpoint = H; // converting this to php data so i can send it by email
Apoint = A; // converting this to php data so i can send it by email


 document.addEventListener("DOMContentLoaded", function(event) {
    document.getElementById('D').style.width = D + 'px';
    document.getElementById('I').style.width = I + 'px';
    document.getElementById('H').style.width = H + 'px';
    document.getElementById('A').style.width = A + 'px';
  });

location.href = "test3.html?D="+ Dpoint + "&I=" + Ipoint + "&H=" + Hpoint + "&A=" + Apoint;


}
// ]]>
</script>
</head> 

<form method="post" action="data.php">
<table>
<tr>
<td>question1</td>
<td><input type="radio" name="D1" value="1"> 1 </td>
<td><input type="radio" name="D1" value="2"> 2 </td>
<td><input type="radio" name="D1" value="3"> 3 </td>
<td><input type="radio" name="D1" value="4"> 4 </td>
<td><input type="radio" name="D1" value="5"> 5 </td>
<td><input type="radio" name="D1" value="6"> 6 </td>
<td><input type="radio" name="D1" value="7"> 7 </td>
<td><input type="radio" name="D1" value="8"> 8 </td>
<td><input type="radio" name="D1" value="9"> 9 </td>
<td><input type="radio" name="D1" value="10"> 10 </td>              
</tr>
<tr>
<td>question2</td>
<td><input type="radio" name="I1" value="1"> 1 </td>
                <td><input type="radio" name="I1" value="2"> 2 </td>
                <td><input type="radio" name="I1" value="3"> 3 </td>
                <td><input type="radio" name="I1" value="4"> 4 </td>
                <td><input type="radio" name="I1" value="5"> 5 </td>
                <td><input type="radio" name="I1" value="6"> 6 </td>
                <td><input type="radio" name="I1" value="7"> 7 </td>
                <td><input type="radio" name="I1" value="8"> 8 </td>
                <td><input type="radio" name="I1" value="9"> 9 </td>
                <td><input type="radio" name="I1" value="10"> 10 </td>
</tr>
<tr>
<td>question3</td>
    <td><input type="radio" name="H1" value="1"> 1 </td>
                <td><input type="radio" name="H1" value="2"> 2 </td>
                <td><input type="radio" name="H1" value="3"> 3 </td>
                <td><input type="radio" name="H1" value="4"> 4 </td>
                <td><input type="radio" name="H1" value="5"> 5 </td>
                <td><input type="radio" name="H1" value="6"> 6 </td>
                <td><input type="radio" name="H1" value="7"> 7 </td>
                <td><input type="radio" name="H1" value="8"> 8 </td>
                <td><input type="radio" name="H1" value="9"> 9 </td>
                <td><input type="radio" name="H1" value="10"> 10 </td>
</tr>
<tr>
                <td><label> question4 </label></td>
                <td><input type="radio" name="A1" value="1"> 1 </td>
                <td><input type="radio" name="A1" value="2"> 2 </td>
                <td><input type="radio" name="A1" value="3"> 3 </td>
                <td><input type="radio" name="A1" value="4"> 4 </td>
                <td><input type="radio" name="A1" value="5"> 5 </td>
                <td><input type="radio" name="A1" value="6"> 6 </td>
                <td><input type="radio" name="A1" value="7"> 7 </td>
                <td><input type="radio" name="A1" value="8"> 8 </td>
                <td><input type="radio" name="A1" value="9"> 9 </td>
                <td><input type="radio" name="A1" value="10"> 10 </td>
        </tr><!-- 14 -->
            <tr>
                <td><label> question5 </label></td>
                <td><input type="radio" name="D2" value="1"> 1 </td>
                <td><input type="radio" name="D2" value="2"> 2 </td>
                <td><input type="radio" name="D2" value="3"> 3 </td>
                <td><input type="radio" name="D2" value="4"> 4 </td>
                <td><input type="radio" name="D2" value="5"> 5 </td>
                <td><input type="radio" name="D2" value="6"> 6 </td>
                <td><input type="radio" name="D2" value="7"> 7 </td>
                <td><input type="radio" name="D2" value="8"> 8 </td>
                <td><input type="radio" name="D2" value="9"> 9 </td>
                <td><input type="radio" name="D2" value="10"> 10 </td>
            </tr><!-- 15 -->
            <tr>
<tr>
<td colspan=2>
<div align="center"><input type="button" value="Score my test" onclick="javascript:tally()"></div>
</td>
</tr>
</table>
</form>
</html>

如何将js数据传递给php?我需要那些js数据转换为PHP并通过电子邮件发送它我该怎么做?我一直在网上寻找解决方案,但我仍然无法得到它可以任何人教我怎么样?或者如果有没有办法使用PHP循环html上的所有无线电值?我是两个代码的新手。

1 个答案:

答案 0 :(得分:0)

你可以通过多种方式传递。

  • 以某种方式尝试,所以通过document.location(重定向) - 将在PHP中的$ _GET
  • 通过AJAX(例如使用jQuery) - 将在PHP中的$ _POST(或$ _GET)
  • by html form - 将在$ _POST中(可能在$ _GET中,但不应该是

要回答你的问题,我必须引导你找到各种答案:

  • 了解从HTML收集数据并使用AJAX(例如在这里使用jQuery)
  • 了解PHP $ _GET,$ _POST,$ _REQUEST
  • 中的请求访问权限
  • 了解用PHP发送邮件(google for&#34; mail php&#34;),有基本(不完美)的代码段