Jump to content

Array with $_POST


xcandiottix

Recommended Posts

My DB has data called A001, A002, A003 etc. I want to echo this data inside of an input form if a value already exists and if not the input field is blank. On pressing submit I want to store any changes, refresh, and then show those changed values in the input fields. The problem I have is:

 

Once the fields have been filled out I want to check everything using an array. So if I filled out input fields with:

 

A001 Hello

A002 World

 

and press submit, I want the array to check if $_POST['A001'] has data, if so store it. Same for A002. But I don't know how to make the $_POST value increment. Here's my idea so far but it doesn't work:

 

//total number of questions
$TNQ = 19;
for ($i = 1; $i<=$TNQ; $i++){
if($i < 10){
	$cell = 'A'. 00 . $i;
	$cellpost = '$_POST['.$cell.']';
	if($cellpost){
		echo $cellpost;
	}
}else if($i < 100){
                $cell = 'A'. 0 . $i;
	$cellpost = '$_POST['.$cell.']';
	if($cellpost){
		echo $cellpost;
	}
       }
}

 

edit== For testing, I am just trying to echo the $_POST data right now. After it works I can save to DB.

 

Any ideas? Or do i need to attack this a whole new way?

Link to comment
https://forums.phpfreaks.com/topic/208137-array-with-_post/
Share on other sites

Without the single quotes I get a PHP error saying unexpected ]. Currently this code will echo:

 

$_POST[A001], $_POST[A002'] ..etc etc

 

NOT the values... just the plain text.

 

I can check if($cellpost) as it is and it will detect it but it's not a good way to do it because $cellpost will always have data since i am setting it equal to '$_POST['.$cell.']';.

 

If I use if($_POST['A001'] that will work and only echo that A001 if it has a value. (!empty or (isset isn't necessary.

Link to comment
https://forums.phpfreaks.com/topic/208137-array-with-_post/#findComment-1087982
Share on other sites

You're over-thinking this. Just build the form from the database query, and make the related fields their own array. Then you can just loop through the sub-array in the $_POST array instead of trying to figure out how to separate that from everything else.

 

while( $row = mysql_fetch_assoc($result) ) {
     echo "<input type=\"text\" name=\"text_fields[{$row['id']}]\" value=\"";
     if( !empty($row['value']) ) {
          echo $row['value'];
     }
     echo "\" />";

Link to comment
https://forums.phpfreaks.com/topic/208137-array-with-_post/#findComment-1087995
Share on other sites

You're over-thinking this. Just build the form from the database query, and make the related fields their own array. Then you can just loop through the sub-array in the $_POST array instead of trying to figure out how to separate that from everything else.

 

while( $row = mysql_fetch_assoc($result) ) {
     echo "<input type=\"text\" name=\"text_fields[{$row['id']}]\" value=\"";
     if( !empty($row['value']) ) {
          echo $row['value'];
     }
     echo "\" />";

 

Well, I need to leave the input fields there incase the user need to comeback later and fill one out. I actutally just figure it out tho, i'll post solved answer below.

Link to comment
https://forums.phpfreaks.com/topic/208137-array-with-_post/#findComment-1087996
Share on other sites

$TNQ = 19;

for ($i = 1; $i<=$TNQ; $i++){

if($i < 10){

$cell = 'A00'.$i;

$cellpost = $_POST[$cell];

if($cellpost){

echo $cellpost;

}

}

}

 

This results in echoing the data from the DB at A001 which is the only field that has a value at current

Link to comment
https://forums.phpfreaks.com/topic/208137-array-with-_post/#findComment-1087998
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.