Click to See Complete Forum and Search --> : [RESOLVED] post value array from newly created textboxes


Jodarecode
10-22-2007, 05:35 PM
Ok, I need to do somthing simular to the php function below except for all the newly created textbox's. some kind of array grabing each value..

first line: "t1_1, t1_2"
newly created: "t2_1, t2_2"
newly created: "t3_1, t3_2"
newly created: "t4_1, t4_2"
newly created: "t5_1, t5_2"
newly created: "t6_1, t6_2"
....and so on


I dont want to have to create a line for who knows how many text boxes the customer creates and have to change each one, it would take to long.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>
Get values from new textbox values
</title>
<script type="text/javascript">

function viewsource() {
alert(document.body.innerHTML);
}
</script>
<script type="text/javascript">

function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var frm=document.form0;
if (!frm.ary) frm.ary=[frm.t1_2];
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);

// numberd row
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);

// Item row
var cellRight1 = row.insertCell(1);
var el1 = document.createElement('input');
el1.type = 'text';
el1.name = 't' + iteration + '_1';
el1.id = 't' + iteration + '_1';
el1.size = 40;
cellRight1.appendChild(el1);

// Price row
var cellRight2 = row.insertCell(2);
var el2 = document.createElement('input');
frm.ary.push(el2);
el2.type = 'text';
el2.value = '';
el2.name = 't' + iteration + '_2';
el2.id = 't' + iteration + '_2';
el2.size = 5;
el2.onkeyup=Calc;
el2.onblur=Calc;
cellRight2.appendChild(el2);
}

</script>

<script type="text/javascript">


function Calc(){
var frm=document.form0;
if (!frm.ary) frm.ary=[frm.t1_2];
var total=0;
for (var zxc0=0;zxc0<frm.ary.length;zxc0++){
if (frm.ary[zxc0].value.length>0&&!isNaN(frm.ary[zxc0].value)) total+=frm.ary[zxc0].value*1;
}
frm.sum.value=total;
}

</script>

</head>
<body>

<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
$me = strip_tags($_SERVER['php_self']);
?>

<form enctype="multipart/form-data" name="form0" id="form0" action="<?php echo $me; ?>" method="POST">
<table border="0" id="tblSample" align="center">
<tr>
<th colspan="3">
<br>
<br>
<br>
Adding rows then adding up values entered(works fine!)
<br><br>
I need to now echo or print using PHP to pull the new values
<br>
from the newly created price textboxes as an email &amp; results page
<hr>

<input type="button" value="Add new textbox" onclick=
"addRowToTable();">
</th>
</tr>
<tr id="cloneid" >
<td>
1 Item/Price
</td>
<td>
<input type="text" name="t1_1" id="t1_1" size="40">

</td>
<td>
<input type="text" name="t1_2" id="t1_2" size="5" value="" onkeyup="Calc();" onblur="Calc();">
</td>
</tr>
</table>
<table border="0" align="center">
<tr>
<td colspan="3" align="center">

<br>
<br>
Total: <input type="text" name="sum" id="sum"><br>
<input type="submit" value="E-Mail">
</td>

</tr>
</table>
</form>

<?php

} else {
error_reporting(0);
{$to = "x";
$subject = "php email from customer to me";
$from = $_POST['x'];

$headers = "From: " . $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";

$msg .= "<body style=\"margin: 5px; font-family: Tahoma, Helvetica, Sans-Serif; font-size: 8pt;\">\n";
$msg .= "<table border=\"0\"><tr><td align=\"right\">";
$msg .= "<b>Item:</b></td><td> " . $_POST['t1_1'] . " \n";
$msg .= "<b>Pice:</b></td><td> " . $_POST['t1_2'] . " \n";
$msg .= "<b>Total:</b></td><td> " . $_POST['sum'] . " \n";
$msg .= "</td></tr></table>";
$msg .= "</body>";

mail($to, $subject, stripslashes($msg), $headers) or die("<p class=\"center\"><strong class=\"red\">ERROR:</strong> Unable To Connect To SMTP Server</p>\n");

echo "<p class=\"center\"><strong class=\"green\">Thank You. Your Order Has Been Processed.</strong></p>\n";

echo "$msg";

}}

?>

</body>
</html>
Any help, much appreciated!!

scragar
10-22-2007, 06:18 PM
you could itterate the $_POST or $_GET set using a foreach.

foreach($_POST as $key => $val){
if(preg_match("/t1_\d+/", $val)){
your code for each box
};
};I know preg_match is not the most efficient solution, but it's the easiest to change and understand.

Jodarecode
10-23-2007, 10:13 AM
Im not sure how to implement this. I've done some research on it and didn't come out with very many examples for codes to test with, I feel like i'm missing something. Its outputing no values. Please help!!

Jodarecode
10-23-2007, 10:56 AM
you could itterate the $_POST or $_GET set using a foreach.

foreach($_POST as $key => $val){
if(preg_match("/t1_\d+/", $val)){
your code for each box
};
};I know preg_match is not the most efficient solution, but it's the easiest to change and understand.

Ok, this is what I understand:
foreach($_POST as $key >= $val) //loops over the $_POST array and gives this a $key value greater or equal to $val

than:
if(preg_match("/t1_\d+/", $val)) //what ever matches t1_(whaterver"d+" ends up being, is value($val)

ok, how do I print the array, as you said "your code for each box"....is it:
print $val;
print $val[0];\\ something I'm missing, I cant figure it out

Thanks for the help, its much appreciated!!

scragar
10-23-2007, 11:02 AM
foreach($_POST as $key => $val){
if(preg_match("/t1_\d+/", $key)){
echo "$key was set to $val<br>";
};
}; look at the result, I have no idea what you want to do with the results, so I can't say much without more info.

Jodarecode
10-23-2007, 11:07 AM
ok, I think I'm getting somewhere with your last post scragar, I'll play around with it and see what I come up with.

Thank you, very much appreciated!!

Jodarecode
10-23-2007, 11:41 AM
I figured it out!!!!
I was able to post every newly created line in what ever fomat I wanted

foreach($_POST as $key => $val){
if(preg_match("/t\d+\_1/", $key)){
$msg .= "<table><tr><td width=\"100\">$val</td>";
};
if(preg_match("/t\d+\_2/", $key)){
$msg .= "<td>$val</td></tr></table>";
};
};

Thanks scragar!!