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
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

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.