简体   繁体   English

在PHP中如何发布在JavaScript中创建的值

[英]In php how to post a value which created in javascript

In a php page ,i created textboxes diynamically in javascript and i want to post these values to another php page how can i get these values ? 在一个php页面中,我使用javascript动态创建了文本框,我想将这些值发布到另一个php页面中,如何获得这些值? And these are my codes ; 这些是我的密码;

while (secenek>0){

var textBoxname = document.createElement('input');
    textBoxname.name = 'textyetiskinname'+secenek;
    textBoxname.id='textyetiskinname'+secenek;
    textBoxname.type = 'text';
    textBoxname.className='selectTravelInputFieldsCarJS';
    document.getElementById("yetiskindiv").appendChild(textBoxname);

secenek--;
}

If that is the entire snippet of script, then you need to build more script to do the ajax function. 如果这是脚本的完整片段,那么您需要构建更多脚本来执行ajax函数。

If you are just trying to grab value of the dynamically created element, then you can use document.getElementById('textBoxname').value; 如果您只是尝试获取动态创建的元素的值,则可以使用document.getElementById('textBoxname').value; as this code will grab the value. 因为此代码将获取值。

set a unique attribute on your input tags: 在输入标签上设置唯一属性:

//at this to your while loop
textBoxname.setAttribute("phpform", "myform");

to be able to find them and then read the form data and send them like this: 以便能够找到它们,然后读取表单数据并像这样发送它们:

function POST_data(callback){
    var myinputs = document.querySelectorAll("input[phpform=myform]");
    var formData = {};
    for(var i=0;i<myinputs.length;i++){
        formData[myinputs[i].getAttribute("name")] = myinputs[i].value;
    }

    req = new XMLHttpRequest();
    req.open("POST", "php_url");
    req.onreadystatechange = function(){
        if(req.readyState==4){
            callback(req.response); 
        }
    };
    req.send(JSON.stringify(formData));
}

call POST_data and give a callback for your callback scenario, like this: 调用POST_data并为您的回调方案提供回调,如下所示:

POST_data(function(response){
    //you can check here if it was successful or not and continue your scenario
});

just be careful, this code is using a sample ajax request, in the real world you'd better use jQuery or at least a cross-browser ajax function. 请注意,此代码使用的是一个示例ajax请求,在现实世界中,您最好使用jQuery或至少一个跨浏览器的ajax函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM