通过可变大小的形式大小、形式

2023-09-10 14:16:12 作者:傲气⒐4埘绱

我有一个形式,是可变尺寸(长度)是从一个MySQL数据库填充的。有4个字段构成用于创建一个按钮(ID,按钮#,名称和价格)的信息。当提交表单我想所有的值保存到MySQL数据库,并在页面中一个成功的消息底部的更新一个div。对于我的所有其他网页我已经使用类似...

xmlhttp.open("GET","myfile.php?a="+val1+"&b="+val2+"&c="+val3+"&d="+val4,true); xmlhttp.send();

的PHP文件保存数据,并生成该消息的分区。 并写入到div ...

 的document.getElementById(txtHint)的innerHTML = xmlhttp.responseText。
 

这工作好我所有的网页,但因为我不知道有多少个领域将是我不能硬code中的xmlhttp.open声明。

我是新来的Ajax和jQuery和认识,必须有一个方法可以轻松地做到这一点,但我一直无法得到任何工作。有人告诉我,我可以用这个

  $每次($('#yourform)serializeArray()函数(){执行console.log(<+ this.name +'>+这.value的+&所述; /+ this.name +>中);});
 
电子元器件图片 名称 符号对照,超全面,求收藏 求扩散 1

和它打印出每个表单元素,但不知道如何得到这个信息,以我的PHP文件,以及如何生成的DIV返回的消息。再次,我是新来的Ajax和jQuery的,所以如果我能得到一些解释,以及我敢肯定它会很长的路要走,以帮助我弄清楚了这一点。

解决方案

希望这有助于:

  $(形式)。递交(函数(){
    $。员额(myfile.php,$(本).serialize()函数(响应){
        的console.log(响应);
    });
});
 

I have a form that is of variable size (length) that is populated from a MySQL db. There are 4 fields that make up the information used to create a button (id, button#, name and price). When the form is submitted I want to save all the values to the MySQl db and update a div at the bottom of the page with a success message. For all my other pages I have used something like...

xmlhttp.open("GET","myfile.php?a="+val1+"&b="+val2+"&c="+val3+"&d="+val4,true);
xmlhttp.send();

The PHP files saves the data and generates the message for the div. and to write to the div...

document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

This work well for all my other pages but since I don't know how many fields there will be I can't hard code the xmlhttp.open statement.

I'm new to ajax and jQuery and know there must be a way to easily do this but I have been unable to get anything working. I was told I could use this

$.each($('#yourform').serializeArray(), function() { console.log(" <" +this.name+ '>' + this.value + "</" + this.name + "> " ); });

and it does print out each form element but not sure how to get this info to my PHP file and how to generate the return message for the div. Again I am new to ajax and jquery so if I could get some explanation as well I'm sure it would go a long way to helping me figure this out.

解决方案

Hope this helps:

$('form').submit(function() {
    $.post("myfile.php", $(this).serialize(), function(response) {
        console.log(response);
    });
});