[英]Submiting a form via json/ajax/jquery
Here is my code 这是我的代码
function generate_clicked()
{
var txt_text_color = $('#txt_text_color').val();
var url='process.php?';
url+='txt_text_color='+encodeURIComponent(txt_text_color);
$.ajax({
url: url,
beforeSend: function ( xhr ) {
xhr.overrideMimeType("application/json; charset=x-user-defined");
}
}).done(function ( data ) {
try{
$('#preview').val(data.css);
$('#my_iframe').srcdoc = data1;
}
catch(err)
{
console.log(err);
}
document.getElementById("my_iframe").src = data.live_preview_html_page;
});
}
This works for my purposes but if I added another form element I would tediousily have to add var example =$('....').val(); 这对我来说是可行的,但是如果我添加另一个表单元素,我将不得不添加var example = $('....')。val();。 and url+='example'+endcodeU..... 和url + ='example'+ endcodeU .....
Which I will be having over 100 elements, then I would retreview them on process with 我将拥有100多个元素,然后在处理过程中重新审查它们
$txt_text_color = $_REQUEST['txt_text_color'];
My question is, how can I serialize this (I think that's what I need to do) so that I don't have to write those two varibles names each time I make a new form object. 我的问题是,我该如何序列化(我认为这是我需要做的),这样我每次创建一个新的表单对象时都不必写这两个变量名称。
I need to save get/post those varibles in process.php to use them. 我需要在process.php中保存获取/发布这些变量以使用它们。
Sorry if this doesn't make sense, I'm still learning. 抱歉,如果这没有意义,我仍在学习。
Try form.serialize() http://api.jquery.com/serialize/ 尝试form.serialize() http://api.jquery.com/serialize/
Your code would look something like this: 您的代码如下所示:
function generate_clicked()
{
var formData = $('#form').serialize();
var url='process.php?';
url+=formData;
$.ajax({
url: url,
beforeSend: function ( xhr ) {
xhr.overrideMimeType("application/json; charset=x-user-defined");
}
}).done(function ( data ) {
try{
$('#preview').val(data.css);
$('#my_iframe').srcdoc = data1;
}
catch(err)
{
console.log(err);
}
document.getElementById("my_iframe").src = data.live_preview_html_page;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.