如何在PHP上使用动态创建的文本框的输入并将它们存储在使用循环的MySQL中? [英] How to take Inputs of Dynamically Created Textbox on php and store them in MySQL using loop?

查看:104
本文介绍了如何在PHP上使用动态创建的文本框的输入并将它们存储在使用循环的MySQL中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Sample.php中,我有

 < html> 
< head>
< title>测试< /标题>
< / head>

< script language =javascript>
var count = 1;

function addRow(divId){



var parentDiv = document.getElementById(divId);

var eleDiv = document.createElement(div);
eleDiv.setAttribute(name,olddiv);
eleDiv.setAttribute(id,olddiv);

var eleText = document.createElement(input);
eleText.setAttribute(type,text);
eleText.setAttribute(name,'textbox'+ count);
eleText.setAttribute(id,textbox+ count);

var eleBtn = document.createElement(input);
eleBtn.setAttribute(type,button);
eleBtn.setAttribute(value,delete);
eleBtn.setAttribute(name,button);
eleBtn.setAttribute(id,button);
eleBtn.setAttribute(onclick,deleteRow('button'));

parentDiv.appendChild(eleDiv);

eleDiv.appendChild(eleText);
eleDiv.appendChild(eleBtn);

var golu = count.toString();
document.getElementById(h)。value = golu;
count ++;



函数deleteRow(tableID){
var div = document.getElementById('olddiv');
if(div){
div.parentNode.removeChild(div);
}
var golu = count.toString();
document.getElementById(h)。value = golu;
}

var golu = count.toString();
document.getElementById(h)。value = golu;



< / script>
< body>
< form action =Page.phpmethod =post>
< input type =buttonvalue =Add Rowonclick =addRow('dataTable')/>
< div id =dataTable>< INPUT type =textname =textbox0id =textbox0/>< / div>
< input typehiddenid =hname =hvalue =0/>
< button type =submitname =submitid =submit>提交< / button>
< / form>

< / body>
< / html>

在Page.php中,我有

 <?php 
include_once('db.php');

$ x = $ _ POST [h];
$ y = intval($ x);
$ z = 0;
while($ z <= $ y){
$ var [z] = $ _ POST ['textbox'] [$ z];
echo$ var [$ z];
$ sql =插入数据值('。$ var [z]。');;

$ query = mysql_query($ sql);
}
?>

我的问题:


  1. 我的目标是接收textbox0,textbox1,..等所有的值。通过在page.php中使用循环。



    但我无法获得所需的结果。实际上,提交后,Pape.php显示为空。收到所有信息后,我也希望它们存储在MySQL数据库中。

  2. 每当我删除一个文本框时,文本框的id序列不会改变。 / p>

请告诉我该怎么做。

解决



无论何时使用JavaScript或jQuery创建文本框,都要保持文本框的计数,假设您在HTML中默认有两个文本框,因此将该计数存储到隐藏字段中,就像您一样:

 <输入类型hiddenid =hname =hvalue =0/> 

然后尝试一下,您正在以错误的方式读取该值:

代替使用 $ var [z] = $ _ POST ['textbox'] [$ z]; 使用 $ var我认为不是编辑每个文本框

> id value,只需将它从HTML中删除并检入PHP代码即可:

 < php 
include_once('db.php');

$ x = $ _ POST [h];
$ y = intval($ x);
$ z = 0;
while($ z< = $ y){
if(isset($ _ POST ['textbox'。$ z])&&!empty($ _ POST ['textbox'。$ z] )){
$ var [z] = $ _ POST ['textbox'。$ z];
echo$ var [$ z];
$ sql =插入数据值('。$ var [z]。');;
$ query = mysql_query($ sql);
}
}
?>

另一种方法可以解决您的两个问题:

test.html:

 < html> 
< title> TEST< / title>
< body>
< form action =test.phpmethod =post>
< input type =textname =demo []value =/>
< input type =textname =demo []value =/>
< input type =textname =demo []value =/>
< input type =textname =demo []value =/>
< input type =textname =demo []value =/>
< input type =submit>
< / form>
< / body>
< / html>

test.php:

 的print_r($ _ POST); 
出口;

输出:

  Array([demo] => Array([0] => zxc [1] => zxc [2] => ewe [3] => ecc [4] => zzx ))


In Sample.php I have

<html>
<head>
<title>Test</title>
</head>

<script language="javascript">
    var count = 1;

function addRow(divId) {



    var parentDiv = document.getElementById(divId);

    var eleDiv = document.createElement("div");
    eleDiv.setAttribute("name", "olddiv");
    eleDiv.setAttribute("id", "olddiv");

    var eleText = document.createElement("input");
    eleText.setAttribute("type", "text");
    eleText.setAttribute("name", 'textbox' + count);
    eleText.setAttribute("id", "textbox" + count);

    var eleBtn = document.createElement("input");
    eleBtn.setAttribute("type", "button");
    eleBtn.setAttribute("value", "delete");
    eleBtn.setAttribute("name", "button");
    eleBtn.setAttribute("id", "button");
    eleBtn.setAttribute("onclick", "deleteRow('button')");

    parentDiv.appendChild(eleDiv);

    eleDiv.appendChild(eleText);
    eleDiv.appendChild(eleBtn);

    var golu=count.toString();
    document.getElementById("h").value= golu;
    count++;

}

function deleteRow(tableID) {
        var div = document.getElementById('olddiv');
        if (div) {
            div.parentNode.removeChild(div);
        }
        var golu=count.toString();
    document.getElementById("h").value= golu;
}

var golu=count.toString();
    document.getElementById("h").value= golu;



</script>
<body>
<form action="Page.php" method="post">
<input type="button" value="Add Row" onclick="addRow('dataTable')" />
<div id="dataTable"><INPUT type="text" name="textbox0" id="textbox0"/></div>
 <input type"hidden" id="h" name="h" value="0"/>
 <button  type="submit" name="submit" id="submit">Submit</button>
</form>

</body>
</html>

In Page.php I have

<?php
 include_once('db.php');

 $x=$_POST["h"];
 $y=intval($x);
 $z=0;
 while($z<=$y){
 $var[z]=$_POST['textbox'][$z];
 echo "$var[$z]";
 $sql="INSERT into Data values('".$var[z]."');";

 $query = mysql_query($sql);
 }
?>

My problems:

  1. My aim is to receive all the value of textbox0, textbox1,..etc. by using loop in page.php.

    But I am not able to get the desired result. Actually after submitting, Pape.php appears empty. After receiving all, I also want them to store in MySQL Database.

  2. Whenever I delete a textbox the sequence of the textboxes' id doesn't change.

Please tell me what to do.

解决方案

You can do this following way.

Whenever you create textbox using JavaScript or jQuery, the maintain the count of the text box, suppose you have two textbox by default on the HTML, so store that count into the hidden field like you did that:

<input type"hidden" id="h" name="h" value="0"/>

Then try this, you are reading the value in wrong way:

Instead of using $var[z]=$_POST['textbox'][$z]; use $var[z]=$_POST['textbox'.$z];.

I think instead of editing each textbox id value, just remove it from HTML and check into the PHP code:

    <?php
     include_once('db.php');

     $x=$_POST["h"];
     $y=intval($x);
     $z=0;
     while($z<=$y){
            if(isset($_POST['textbox'.$z]) && !empty($_POST['textbox'.$z])){
                     $var[z]=$_POST['textbox'.$z];
                     echo "$var[$z]";
                     $sql="INSERT into the Data values('".$var[z]."');";
                     $query=mysql_query($sql);
            }
     }
    ?>

Another way, to solve both of your problems :)

test.html:

<html>
    <title>TEST</title>
    <body>
        <form action="test.php" method="post">
        <input type="text" name="demo[]" value=""/>
        <input type="text" name="demo[]" value=""/>
        <input type="text" name="demo[]" value=""/>
        <input type="text" name="demo[]" value=""/>
        <input type="text" name="demo[]" value=""/>
        <input type="submit">
        </form>
    </body>
</html>

test.php:

print_r($_POST);
exit;

output:

Array ( [demo] => Array ( [0] => zxc [1] => zxc [2] => ewe [3] => ecc [4] => zzx ) )

这篇关于如何在PHP上使用动态创建的文本框的输入并将它们存储在使用循环的MySQL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆