Jump to content

[SOLVED] Please help with syntax, foreach..


fmpros

Recommended Posts

Hello,

 

I've got a dynamic value list using php and FileMaker Pro.  I'm trying to include the $POST value so that the user doesn't have to re-enter their info if the form returns an error.

 

Here is the code:

 

$records1 = $result1->getRecords();

$session1_popup = '<select name="session1" id="session1"><option value="" label=""</option>';

foreach($records1 as $record1) {

  $value1 = $record1->getField('field_a');

  $label1 = $record1->getField('field_b');

  $session1_popup.= '<option value="' . $value1 . '">' . $label1 . '</option>';

}

$session1_popup.= '</select>';

 

 

The above code works as expected.  What I'd like to do now is add a $POST value to line #2.  I'm not quite sure what syntax to use.  Something like this?

 

$session1_popup = '<select name="session1" id="session1" value="<?php '.echo $_POST[ 'session1' ].'?>"><option value="" label=""</option>'; :facewall:

 

Any insight would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/170183-solved-please-help-with-syntax-foreach/
Share on other sites

You don't need to use the echo statement since it's already within a php string.

 

Try this

$session1 = mysql_real_escape_string($_POST[ 'session1' ]);
$session1_popup = <<<HTML
<select name="session1" id="session1" value="$session1"><option value="" label=""></option>
HTML;

 

Edit: I used heredoc in the code above.

 

http://us2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Hey, thanks for the reply!  I tried inserting the code exactly as shown below, with no luck.  When the form returned an error, the field was reset and did not retain the original value.  Any other ideas?

 

Thanks again

 

 

$session1 = $_POST[ 'session1' ];
$records1 = $result1->getRecords();
$session1_popup = <<<HTML
<select name="session1" id="session1" value="$session1"><option value="" label=""></option>
HTML;

 foreach($records1 as $record1) {
  		$value1 = $record1->getField('_kf_workshop');
  		$label1 = $record1->getField('PHP_Workshop::Title');
  	$session1_popup.= '<option value="' . $value1 . '">' . $label1 . '</option>';
}
$session1_popup.= '</select>';

Oh, that's because we're trying to assign a value to a select box using php (can't be done).  You have to write the script to loop through the options and highlight the correct option using selected="selected".  Otherwise you can use an ajax library or jquery to do it.

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.