无法从javascript发送到PHP

时间:2012-07-12 10:32:17

标签: javascript-events

我正在创建一个动态表单。我使用以下javascript

var labels=[];
function add(type) {
     var element = document.createElement("input");
     var label=prompt("Enter the name for lable","label");
     labels.push(label);
     alert(labels);
     document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+label;
     element.setAttribute("type", type);


     var rohit = document.getElementById("raj");
    rohit.appendChild(element);
    document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+"<br/>";

}
function add1(type) {
     var element = document.createElement("input");
     var label=prompt("Enter the name for lable","label");
     document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+label+": : ";
     element.setAttribute("type", type);
     var no=prompt("Total no of fields in radio button ");
     labels.push(label);
     alert(labels);
     for(var i=1;i<=no;i++)
     {
        var val=prompt('Enter value');     

     document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+val+" ";
     var rohit = document.getElementById("raj");
    rohit.appendChild(element);
     }
     document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+"<br/>";


}
function add2(type) {
     var element = document.createElement("textArea");
     var label=prompt("Enter the name for lable","label");
     document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+label;
     element.setAttribute("type", type);
     labels.push(label);
     alert(labels);

     var col=prompt('Enter the no of columns');
     element.setAttribute("cols",col);
     var row=prompt('Enter the no of rows');
     element.setAttribute("rows",row);

     var rohit = document.getElementById("raj");
    rohit.appendChild(element);
    document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+"<br/>";

}

function callfunc()
{

    var http = new XMLHttpRequest(); 
    var name=prompt('enter name of the form');
    //var name="rohit";

    alert(name);
    var s = document.getElementById('raj').innerHTML;
    alert(s);

    http.open("POST", "index1.php?name"+name, true);
    http.onreadystatechange = function() 
    {
        if(http.readyState == 4 && http.status == 200) 
        {
    alert('sfsf');
        alert(responseText);
        }
    }

    http.send();
    return false;
}

然后我将此代码用于以下表单

<div id="menu" style="height:200px;width:100px;float:left;">
<input type="button" value="Text Field" onclick="add('text')">

<br/>
 <input type="button" value="Radio Button" onclick="add1('radio')"><br/>
 <input type="button" value="Check Box" onclick="add1('checkbox')"><br/>
 <input type="button" value="Text Area" onclick="add2('textarea')"><br/>
</div>

<div id="content" style="height:200px;width:400px;float:left;">
<form action="index1.php" onsubmit="callfunc()">
<span id="raj"></span>
<input type="submit" value="submit"></div>

但是当我点击提交时,它不会重定向到index1.php?name =但它只能重定向到index1.php。意味着我想将数据从javascript发布到php,但我无法使用此脚本。我该怎么办?

0 个答案:

没有答案