Page 1 of 1
create var name dynamically based on a number?
Posted: Sat May 29, 2010 8:45 pm
by iansane
Hi,
I need to make php vars to retrieve from POST. The form is created dynamically based on a number entered in another form. So I can't create a specific number of variables. So I'm wondering can I do something like this?
Code: Select all
$count = 5;
for($i = 1; $i <= $count; $i++){
//code to make and name variable
}
so I end up with an array full of variables listed like this
$var1
$var2
$var3
$var4
$var5
And it would change how many vars it makes according to the number in $count
Re: create var name dynamically based on a number?
Posted: Sat May 29, 2010 8:58 pm
by phdatabase
Yes, you can do that. Don't forget that $_POST is an array. So you can count($_post) (don't forget submit value!). Or you could do a foreach($_POST as..) and feed all the variables into an array.
Re: create var name dynamically based on a number?
Posted: Sat May 29, 2010 9:59 pm
by iansane
Thanks phdatabase,
I'm not sure yet how to use the foreach() function but I'm looking at it.
I sat down with pen and paper and came up with this though.
Code: Select all
$ACArray = array();
$ACCount = 3;
for($i =1; $i <= $ACCount; $i++){
$postString = "AC" + $i;
array_push($ACArray, $_POST[$postString]);
}
Should give me $ACArray(0->AC1, 1->AC2, 2->AC3)
I'm about to try it and see.
Then for creating a sql statement I was thinking
Code: Select all
for($i = 0, $i <= count($ACArray); $i++){
$ACValue = ACArray[$i];
$sql = "INSERT into tblname (part) VALUES('$ACValue')";
}
Of course there are other values that are allready present on the same page so I'm hoping I can use them in the for() statement like global variables.
Does this look like the right way to do things other than possibly trying to figure out the foreach() function?
Re: create var name dynamically based on a number?
Posted: Sun May 30, 2010 10:52 am
by phdatabase
Sorry> I totally misunderstood your question! I thought you were trying to READ a $_POST.
You may easily send an array through the $_POST array. If the form does not create the array, use a 'hidden' type to transport it.
<input type='hidden' name='myArray' value='$arrayToSend' />
Then on the back end the array will be in $_POST['myArray']
Again, sorry and I hope this is what you wanted.
Re: create var name dynamically based on a number?
Posted: Sun May 30, 2010 11:08 am
by phdatabase
You can't do it that way. Try
Code: Select all
for($i =1; $i <= $ACCount; $i++){
$postArray[] = "AC" + $i;
}
<input type='hidden' name='myArray' value=$postArray />
Re: create var name dynamically based on a number?
Posted: Sun May 30, 2010 2:04 pm
by iansane
I guess I should start by explaining better.
I have a form in which only part of the data needs to be sent to php so the popup for editing can open and build another form dynamically depending on what was entered in the main form. Then the user is returned to finish filling out the main form. This is how I accomplished that part.
ajax code in ticket.php:
Code: Select all
//var passing
// Get the HTTP Object
function getHTTPObject(){
if (window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest){
return new XMLHttpRequest();
}
else {
alert("Your browser does not support AJAX.");
return null;
}
}
// Not used here but deletion causes doWork() not to function
//need to figure out why so I can delete this part. Found this on the Internet
function setOutput(){
if(httpObject.readyState == 4){
document.getElementById('outputText').value = httpObject.responseText;
}
}
// Main function for getting and passing var
function doWork(ID){
httpObject = getHTTPObject();
if (httpObject != null) {
var httpObjectString = "../includes/ticketVarsInc.php?" + ID + "=";
httpObject.open("GET", httpObjectString + document.getElementById(ID).value, true);
httpObject.send(null);
/*next line commented out because only used for output
httpObject.onreadystatechange = setOutput;*/
}
}
var httpObject = null;
Submit button code in ticket.php:
Code: Select all
//calls ajax function and opens popup
<input type="button" value="add part numbers" onclick="doWork('equipQuantityAnalogConverter');doWork('equipQuantityDCT');
doWork('equipQuantityHDTVConverter');doWork('equipQuantityDVRService');popUp();"></input>
ticketVarsInc.php:
Code: Select all
<?php
session_start();
?>
<?php
/*collect and count equip quantities and place them in $HTTP_SESSION_VARS*/
if (isset($_GET['equipQuantityAnalogConverter'])){
$ACCount = $_GET['equipQuantityAnalogConverter'];
$HTTP_SESSION_VARS['ACCount'] = $ACCount;
}
if (isset($_GET['equipQuantityDCT'])){
$DCTCount = $_GET['equipQuantityDCT'];
$HTTP_SESSION_VARS['DCTCount'] = $DCTCount;
}
if (isset($_GET['equipQuantityHDTVConverter'])){
$HDTCount = $_GET['equipQuantityHDTVConverter'];
$HTTP_SESSION_VARS['HDTCount'] = $HDTCount;
}
if (isset($_GET['equipQuantityDVRService'])){
$DVRCount = $_GET['equipQuantityDVRService'];
$HTTP_SESSION_VARS['DVRCount'] = $DVRCount;
}
?>
<?php
/*collect data from ticketPopupInc.php*/
//This is where I need to get the data from the popup
//no way of the system knowing how many fields without using $ACCount to loop through the array
?>
ticketPopupInc.php:
Code: Select all
<?php
session_start();
?>
<html>
<head><title>Equipment Numbers</title>
</head>
<body>
<script type="text/javascript">
//This function not used at this point. Replace with ajax doWork function from ticket.php?
function submitForm(){
document.forms["equipForm"].submit();
}
function show_alert()
{
alert("Equipment Succesfully Added To Inventory!");
}
</script>
<form name="equipForm" id="equipForm" action="ticketVarsInc.php" method="POST">
<?php
/*get vars from session*/
$ACCounter = $HTTP_SESSION_VARS['ACCount'];
$DCTCounter = $HTTP_SESSION_VARS['DCTCount'];
$HDTCounter = $HTTP_SESSION_VARS['HDTCount'];
$DVRCounter = $HTTP_SESSION_VARS['DVRCount'];
/*layout form entries for equip#'s based on quantity from "ticket.php" form*/
echo '<table border="1px">';
if ($ACCounter != 0){
$ACFormCount = 0;
for ($i = 1; $i <= $ACCounter; $i++){
$ACFormCount++;
echo '<tr>';
echo '<td>';
echo 'Analog Converter ';
echo $i;
echo '</td>';
echo '<td>';
echo '<input type="text" size="25" maxlength="25" name="AC';
echo $i;
echo '"></input>';
echo '</td>';
echo '</tr>';
}
}
if ($DCTCounter != 0){
for ($i = 1; $i <= $DCTCounter; $i++){
$DCTFormCount++;
echo '<tr>';
echo '<td>';
echo 'DCT ';
echo $i;
echo '</td>';
echo '<td>';
echo '<input type="text" size="25" maxlength="25" name="DCT';
echo $i;
echo '"></input>';
echo '</td>';
echo '</tr>';
}
}
if ($HDTCounter != 0){
for ($i = 1; $i <= $HDTCounter; $i++){
$HDTFormCount++;
echo '<tr>';
echo '<td>';
echo 'HDT ';
echo $i;
echo '</td>';
echo '<td>';
echo '<input type="text" size="25" maxlength="25" name="HDT';
echo $i;
echo '"></input>';
echo '</td>';
echo '</tr>';
}
}
if ($DVRCounter != 0){
for ($i = 1; $i <= $DVRCounter; $i++){
$DVRFormCount++;
echo '<tr>';
echo '<td>';
echo 'DVR ';
echo $i;
echo '</td>';
echo '<td>';
echo '<input type="text" size="25" maxlength="25" name="DVR';
echo $i;
echo '"></input>';
echo '</td>';
echo '</tr>';
}
}
echo '</table>';
echo '<br />';
?>
<input type="button" value="Add" name="addNumbers" id="addNumbers" onclick="show_alert(); window.close();"></input>
</form>
</body>
</html>
As you can see the popup is created dynamically so there is no way once I submit it for the app to know how many fields were submitted to do the retrieval on the other end back in the ticketVarsInc.php page without using $ACCount, $DCTCount, $HDTCount, and $DVRCount as parameters in loops with arrays.
These are only 4 items out of 60 or 70 other items on the main ticket.php form so I don't want to use a regular submit at this point. The final submit button is at the end of the main form after all data is collected.
Everything works great up to this point so I just have to use ajax again to pass these fields into an array and then itterate through the array for my sql statement back in the ticketVarsInc.php page or in a seperate ticketFunctions.php page.
Maybe I can unset $_Get and then just reuse it the same way. Then count($_Get) should be an accurate count for building my sql query from?
Re: create var name dynamically based on a number?
Posted: Sun May 30, 2010 2:48 pm
by phdatabase
So as I understand it, you have all the data from the popup and the main form and you just need to incorporate the unknown number of items into the main form to submit them all at once?
Re: create var name dynamically based on a number?
Posted: Sun May 30, 2010 3:13 pm
by iansane
yep that's pretty much it.
I need to get the unknown out of $_Get if I use ajax to put them into $_GET
I think in talking through this I've about got it figured out but any suggestions for the right way to do this would be great.
I think I'll gather all the information from the popup and the main form and the last thing in the onclic of the main submit button be a call to the function that brings it all together and builds the sql statement.
Re: create var name dynamically based on a number?
Posted: Sun May 30, 2010 3:18 pm
by phdatabase
So your pop-up form returns a $_GET array?
Re: create var name dynamically based on a number?
Posted: Sun May 30, 2010 3:54 pm
by iansane
It will if I use ajax to do it. I just wasn't sure if there is another way that is better. The data here isn't really sensitive but I don't know enough about ajax to know if it is dangerous because it makes a connection between the client side and server side.
That concern aside, yes there will be a $GET array sent from the popup