Page 1 of 1

php within HTML form within php...

Posted: Tue Apr 20, 2010 11:22 am
by swoop80
Hi. I'm a newbie on this forum. I used PHP some a few years ago and am trying to get back into it now. However, back when I used it, I don't think I ever had the occasion to do what I'm asking about in this post...

The LAST statement in the "while" loop in the program is used to create a drop-down list of country codes in the html form. Within each "option" tag for this country drop down, I want a bit of PHP too - and that is to just test to see if a value had been entered before we went off to edit the submitted values...and if so, I'll retain that user's entered value when the screen is displayed again (when an edit error is detected, the form is re-displayed so the user can correct whatever errors may exist).

The script is just in a rough 'test' mode right now. For instance, it doesn't even have any real edits in it (I make each 'submit' fail as if there were edit issues for now).

I want to focus on the creation of the country code drop-down list in this thread.

I think that the 'problem' is that the php code that is inserted WITHIN the <option> tags for the country code drop-down are not being recognized as code... (note that the drop down list that is included in the form just before this 'country code' stuff also has embedded php statements to test for saveoff field values - for the same reason as what I'm attempting with the php-constructed country code drop-down - and the embedded php statements in that prior drop down seem to work just fine).

I'd appreciate your help in determining what I should do differently when programatically building the country code drop down - specifically on the point of getting that embedded PHP (which tests $savecou) to work - and to be recognized as code.

One last point: the "input file" that is read in this script (called "countries.txt") contains 4 "records" - and each record has 3 comma-delimitted fields - the first of which is the textual 'country name'.

To try out the code as it exists today, you can see: http://bandyak.co.cc/persc.php

Your assistance is GREATLY appreciated!

here is the script in it's current (rough) state:

Code: Select all

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

<?
        $htmlco = 'COUNTRY:<select name=\"Country\">';
        $myFile = 'countries.txt';
        $fh = fopen($myFile, "r");
        while(! feof($fh))
          {
          $theData = fgets($fh);
          $piece = explode(",", $theData);
          [b]$htmlco = $htmlco.'<option value ="'.$piece[0].'"'.' <?php if ($savecou=="'.$piece[0].'") print "selected"; ?> >'.$piece[0].'</option>';[/b]
          }
        fclose($fh);
        $htmlco = $htmlco.'</select><br />';
        echo $htmlco;
?>

<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;
}
?> 

Re: php within HTML form within php...

Posted: Tue Apr 20, 2010 4:48 pm
by Christopher
HTML can only be in strings in PHP blocks. The problem is this line:

Code: Select all

          <strong>$htmlco =  $htmlco.'<option value ="'.$piece[0].'"'.' <?php if ($savecou=="'.$piece[0].'") print "selected"; ?> >'.$piece[0].'</option>';</strong>
It should be:

Code: Select all

          $htmlco =  $htmlco.'<option value ="'.$piece[0].'"'.' <?php if ($savecou=="'.$piece[0].'") print "selected"; ?> ><strong>'.$piece[0].'</strong></option>';

Re: php within HTML form within php...

Posted: Tue Apr 20, 2010 10:49 pm
by swoop80
Hi Christopher. I apologize for being confusing.

The "<strong>" and "</strong>" tags are simply an artifact of my posting on the forum...

In other words, those aren't really in my code at all...

When I removed those tags from both the line in my post AND from your suggested line, the resultant code is identical - meaning that your proposal isn't actually helping me with the problem I'm trying to work on.

Again - sorry I was confusing. Your time is appreciated. Do you have some other thought about what I should be doing different? One thing that might be helpful: when you actually click on my 'bandyak' link, you can then "view page source" and you will see that the PHP that is embedded in the programmatically generated drop down doesn't look like the drop down that is just ahead of that place in the logic (in the one that is just ahead, the embedded php doesn't even show up when you "view page source"...).

Thank you.

Gary

Re: php within HTML form within php...

Posted: Wed Apr 21, 2010 9:24 am
by swoop80
Hi. Anyone have any thoughts on this issue? Appreciate your time.

Re: php within HTML form within php...

Posted: Thu Apr 22, 2010 7:45 am
by dmsundar
Hope you can alter the code like below....


<option value="Verizon" <?php if ($savecarr == 'Verizon') print 'selected'; ?>>Verizon</option></select>

Country:<select name="Country">

Code: Select all

<?
        $myFile = 'countries.txt';
        $fh = fopen($myFile, "r");
        while(! feof($fh))
          {
          $theData = fgets($fh);
          $piece = explode(",", $theData);
          if($savecou==$piece[0])
              echo "<option value=" . $piece[0] ." selected >" . $piece[0] ."</option>";
          else
              echo "<option value=" . $piece[0] .">" . $piece[0] . "</option>";
          }
        fclose($fh);
        echo "</select><br />";
?>
<input type="submit" value="Update Now!" name="submit">
<INPUT TYPE=RESET VALUE="Cancel">
.
.
.
_____________________________________________________
"The Only way out always is through" :)

Re: php within HTML form within php...

Posted: Thu Apr 22, 2010 7:56 am
by swoop80
Thank you. My work around is similar to this. I was hoping to learn more about why the other wouldn't work - but will settle for just getting 'something' workable. Again - I thank you for your time and help!

Re: php within HTML form within php...

Posted: Thu Apr 22, 2010 8:08 am
by dmsundar
for php interpreter
$htmlco is a variable which needs to be echoed...it wont parse the php code inside the variable...

its obvious

to be clear....the php codes we are sharing in this posts wont be processed by the page...

___________________________________________________________
The Only way out is always through. :)