Hi. Newbie here. I learned PHP a while back and then didn't use it for a while, and now I'm trying to get back into the swing. However, I'm not sure I ever tried before to do what I'm trying now...
In the below displayed code, I'm using form-embedded PHP to produce a "country" drop-down in a simple HTML form.
It's sort of 'working', but in building each "<option..." statement within that created drop-down, I'm also embedding PHP within that stuff (to test any saved value of the field and retain the highlighted/selected value of this field if the form fails edit...this way, the user won't have to re-key all field values) - and that innermost PHP isn't being recognized, I think.
Note: the 'file' that is being read for this test is a very simple file with only 4 records in it. Each of these records has 3 fields (country name, country number, and country code...and these three fields are delimitted by commas).
Here is the code - and the RED stuff is that which is not quite working: (obviously, it's in a test mode - with edits not even in place yet)
TIA, folks.
<?php
$Mobile = $_POST["Mobile"];
$Carrier = $_POST["Carrier"];
$Country = $_POST["Country"];
top:
if (!isset($_POST['submit'])) { // if not yet submitted then display the main form
?>
<html>
<head>
<title>title stuff</title>
</head>
<body bgcolor="#203302" text="#0EEFFF" link="#FFFFFF" vlink="#CCCCCC" alink="#33FFFF">
<div id="Layer1" style="position:absolute; z-index:3; left: 17%; top: 20%;">
<form method="post" action="<?php echo $PHP_SELF;?>" onReset="return confirm('Do you really want to reset the form?')"
MOBILE#:<input type="text" size="10" maxlength="10" name="Mobile" value="<?php echo $savemobl;?>">
CARRIER:<select name="Carrier">
<option value="" <?php if ($savecarr == '') print 'selected'; ?>> </option>
<option value="ATT" <?php if ($savecarr == 'ATT') print 'selected'; ?>>AT&T</option>
<option value="Sprint" <?php if ($savecarr == 'Sprint') print 'selected'; ?>>Sprint</option>
<option value="TMobile" <?php if ($savecarr == 'TMobile') print 'selected'; ?>>TMobile</option>
<option value="Verizon" <?php if ($savecarr == 'Verizon') print 'selected'; ?>>Verizon</option></select>
[COLOR="Red"]<?
$htmlco = 'COUNTRY:<select name=\"Country\">';
$myFile = 'countries.txt';
$fh = fopen($myFile, "r");
while(! feof($fh))
{
$theData = fgets($fh);
$piece = explode(",", $theData);
$htmlco = $htmlco.'<option value ="'.$piece[0].'"'.' <?php if ($savecou=="'.$piece[0].'") print "selected"; ?> >'.$piece[0].'</option>';
}
fclose($fh);
$htmlco = $htmlco.'</select><br />';
echo $htmlco;
?>[/COLOR]
<input type="submit" value="Update Now!" name="submit">
<INPUT TYPE=RESET VALUE="Cancel">
</form>
</div>
<?
} else {
$e = '';
if (editpers($e) != '') {
?>
<html>
<head>
<title></title>
</head>
<body>
<form method=get action="whereveryouwant.com" onSubmit="return false;">
(EXPERIMENTING ONLY HERE - EDITS FORCED TO FAIL) THERE IS A PROBLEM WITH YOUR INFO<br />
<input type="submit">
</form>
<?
$savemobl = $Mobile;
$savecarr = $Carrier;
$savecou = $Country;
unset($_POST['submit']);
goto top;
}
else {
echo "Hello, ".$Fname." ".$Lname.".<br />";
echo "You are ".$gender.", and you like ";
foreach ($genre as $g) { echo $g."<br />"; }
echo "<i>".$quote."</i><br />";
}
}
?>
<?php
function editpers($epmsg)
{
$epmsg = 'state field is incorrect, please try again';
return $epmsg;
}
?>