Jump to content

Can't access array


Hatdrawn

Recommended Posts

I'll try to explain best I can, let me know if I'm not clear please.

 

I created an array

 

$arr_email = array();

 

then I have an if statement that does several things, everything works, but the focus is adding to the array $arr_email.

 

if($_POST['Submit_Auto'] || !$_POST['Email_Auto']){
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"><table class="verify"><tr><td><h3 style="margin-bottom:5px;">Auto Insurance Form</h3></td></tr><tr><td style="margin:0;padding:0;font-size:smaller;font-style:italic;">Please verify your information</td></tr>';
  foreach($_POST as $var => $value)
  {
	  if(in_array($var, $arr_auto)){
	  echo '<tr>';				
	  echo '<td class="output">'.$var . '</td><td><span class="userInput">' . $value . '</span></td>';
	  $arr_email[$var] .= $value;
	  }
	  echo '</tr>';
  }
print_r($arr_email);
echo '<tr><td><input type="button" value="Go Back" onclick="history.go(-1);return true;"/></td><td><input type="submit" name="Email_Auto" value="Request Quote"/></td></tr></table></form>';
}

 

I have a page that displays the information from a form on the page before. This works fine. I added a submit button to accept this information and send it. The submit button is in a form that has the action $_SERVER['PHP_SELF'] the name of the submit button is Email_Auto. Now i have a following else/if statement where I try to access $arr_email because I use it to create the body of my email.

 

else if($_POST['Email_Auto']){
$to = "[email protected]";
$headers = "From: Think!nsure Auto Insurance Form";
$subject = "Auto Insurance Quote Request";
$body = "The user submitted the following:\n\n";
foreach($arr_email as $var => $value){ 
	if(in_array($var, $arr_auto)){
		$body .= sprintf("%20s : %s\n",$var,$value); 
	}
}

  if (mail($to, $subject, $body, $headers)) {
	  echo("<p>Message successfully sent!</p>".$body);
	  print_r($arr_email);
  } else {
	  echo("<p>Message delivery failed...</p>".$body);
	  print_r($arr_email);
  }
}

 

The print_r($arr_email); in the if statement shows me what I want and the others do not, so it seems that once I hit the Email_Auto submit it clears the array. I might have a very wrong idea of what's going on here.

 

I get the emails, but they only have the default body.

 

Please help and please forgive me if I'm a complete idiot.

I can post all the php if necessary but this is all the relevant php.

 

Thanks,

 

Hatdrawn

 

Link to comment
https://forums.phpfreaks.com/topic/161840-cant-access-array/
Share on other sites

With the two pieces of code you have provided, and assuming they are they in the same if/else statement, $arr_email only gets added to if $_POST['Submit_Auto'] is true and $_POST['Email_Auto'] is false.

 

So when Email_Auto is submitted, it goes into the elseif statement where you are only trying to read from the $arr_email array.

Link to comment
https://forums.phpfreaks.com/topic/161840-cant-access-array/#findComment-853903
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.