Jump to content

[SOLVED] Form and PHP in the same file


Recommended Posts

I prefer using the REQUEST_METHOD...

 

<?php
  if($_SERVER['REQUEST_METHOD'] == 'POST'){
    //Form Submitted

    //Do stuff here

    //Send the user to a thankyou page or back to self
    //This will keep from double posting with the refresh button
    header('Location: somepage.php');
    exit;
  }
?>
<form method="POST">
  By leaving the ACTION attribute off the form, it will automatically post to itself.
  FORM ELEMENTS HERE
  <input type="submit" value="Submit Form" />
</form>

Link to comment
Share on other sites

Well I am getting "unexpected $end " in file "xxxxx" at line number xx

 

What I am trying to do is this :

 

I have a .html file with a form which allows the user to select a gallery. Once thats done action statement in form transfers control to a php file which then displays the gallery by reading image links from a text file. Which text file it chooses depends on the gallery number the user selected.

 

Can anybody give the proper code or stub so that I can fill in the details ?

 

Should the .php have a <html> and </html> tag as it contains form tags ? Also I want something to be displayed on the window title bar , kind of like what <title> used to do in html.

Link to comment
Share on other sites

Error message is :

 

Parse error: syntax error, unexpected $end in /hsphere/local/home/meltingm/testingground.meltingmasala.com/galleries/display gallery 2.php on line 52

 

 

Code is :

 

 

<?

if(isset($_POST['submit']))
{
$gallery_number=$_POST['gallery_number'];

$file_name="gallery".$gallery_number."."."txt";

$my_File=$file_name;

$fh = fopen($my_File,'r');
$i=1;
echo "<center>";
echo "<table border=2>";

while (!feof($fh))
{
$theData=fgets($fh);

echo "<tr>";
echo "<td>";
echo "<center> <b>Image : $i </b>";
$i=$i+1;
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<center>";
echo "<img src=\"" . $theData ."\">";
echo "</center>";
echo "</td>";
echo "</tr>";
echo "<br>";
};
echo "</table>";
fclose($fh);
?>
}


<form action="display_gallery_version_two.php" method="post"> 

//"display_gallery_version_two is the name of this php file in which the form is embedded.

<select name="gallery_number">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<input type="submit" value="Visit Gallery"/>
</form>

 

 

I want one more form displayed which allows the user to choose a page number (for some galleries only) and the filename will be created by combining the gallery number and the page number.Some galleries have only one page , other have many pages. If the number of image links is more than 10 , it goes into the second page. Thus actually the form should display the page numbers option only if there are more than 10 image links. Is there anyway we can configure the form drop down menu or the radio buttons so that the number of page numbers options displayed is dependent on the number of pages existing ? ( Info about this will be available in the file itself)

Link to comment
Share on other sites

Your problem was a close brace outside of the php tags...see comments in code

<?php
if(isset($_POST['submit']))
{
  $gallery_number=$_POST['gallery_number'];

  $file_name="gallery".$gallery_number."."."txt";

  $my_File=$file_name;

  $fh = fopen($my_File,'r');
  $i=1;
  echo "<center>";
  echo "<table border=2>";

  while (!feof($fh))
  {
    $theData=fgets($fh);

    echo "<tr>";
    echo "<td>";
    echo "<center> <b>Image : $i </b>";
    $i=$i+1;
    echo "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>";
    echo "<center>";
    echo "<img src=\"" . $theData ."\">";
    echo "</center>";
    echo "</td>";
    echo "</tr>";
    echo "<br>";
  } //Removed semicolon here
  echo "</table>";
  fclose($fh);
} //Your close brace was outside the php tags
?>


<form action="display_gallery_version_two.php" method="post"> 

//"display_gallery_version_two is the name of this php file in which the form is embedded.

<select name="gallery_number">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<input type="submit" value="Visit Gallery"/>
</form>

 

 

also, if I were doing this, here is what my code would look like:

<?php
  if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $file = "gallery{$_POST['gallery_number']}.txt";
    echo '<table border="2" style="margin:auto;">';
    foreach(file($file) as $n => $line){
      echo '<tr><td style="text-align:center;"><b>Image : '.($i + 1).'</b></td></tr>';
      echo '<tr><td style="text-align:center;"><img src="'.$line.'"></td></tr>';
    }
    echo "</table>";
  }
?>
<form method="get"> 
  <select name="gallery_number">
    <option>1</option>
    <option>2</option>
    <option>3</option>
  </select>
  <input type="submit" value="Visit Gallery" />
</form>

i used the file() function instead of fopen to make things simpler. Got rid of the form action (it will default to itself). Made the form use GET instead so the gallery number is in the url, and a couple other things to shorten it up.

Link to comment
Share on other sites

Excellent Rhodesa ! It worked like magic. However you made a mistake using GET method in form while using POST method while trying to get the value. I was temporarily stumped by the error messages , but managed to debug it. It works fine.

 

However , I have a few questions. How does the position of the form elements in the php file affect its position on the screen. In this example , first time the form drop down menu is displayed in the top left hand corner of the  screen. Then after the gallery is selected and the images are displayed the form drop down menu goes to the bottom of the screen. Can I permanently have it at a place of my choice say top-center of the page everytime a gallery is displayed ?

 

Also some galleries have more than one page. That information will be at the end of each gallery file.So I would like to display another form element which will allow the user to select the page as well. But this element is displayed only if the gallery has more than one page. Otherwise it is not. And I would like it to be always displayed on the top-right corner of the screen.

 

Also is it possible to display number of galleries in the drop down menu as per the actual number of galleries available ? As of now there are three galleries and the drop down menu lists 3 galleries. That should automatically change as new galleries are added. Is it possible to do that ?

 

 

Link to comment
Share on other sites

Also as of now I am getting the following error message.

 

Warning: file(gallery.txt) [function.file]: failed to open stream: No such file or directory in /hsphere/local/home/meltingm/testingground.meltingmasala.com/galleries 2/display gallery.php on line 5

 

Warning: Invalid argument supplied for foreach() in /hsphere/local/home/meltingm/testingground.meltingmasala.com/galleries 2/display gallery.php on line 5

Link to comment
Share on other sites

Good catch, forgot to update that. But, you can't really test for REQUEST_METHOD == GET, because all page calls (unless they are POST) are GET. Even if you aren't submitting a form. The code below should get rid of that warning.

 

Also, the form will be wherever you tell it to be. If you want the form to always be above the table, just move the PHP code that prints the table further down, after the form.

 

<form method="get"> 
  <select name="gid">
    <option>1</option>
    <option>2</option>
    <option>3</option>
  </select>
  <input type="submit" value="Visit Gallery" />
</form>
<?php
  if(isset($_GET['gid'])){
    $file = "gallery{$_GET['gid']}.txt";
    echo '<table border="2" style="margin:auto;">';
    foreach(file($file) as $n => $line){
      echo '<tr><td style="text-align:center;"><b>Image : '.($i + 1).'</b></td></tr>';
      echo '<tr><td style="text-align:center;"><img src="'.$line.'"></td></tr>';
    }
    echo "</table>";
  }
?>

Link to comment
Share on other sites

Thanks Rhodesa , you have been awesome ! It works like clockwork. Now is it possible to use DIV tags to position forms in any particular place in the displayed file ? Also can a particular form be displayed based on conditional statements ? I mean if an if(condition) resolved to true , a particular form would be displayed , else another form would be displayed ! And if there were musltiple forms , and if a form was submitted , would the control immediately be transferred to the same php entry point or can we haev multiple entry points for different forms ?

Link to comment
Share on other sites

oops...sorry, i used $n for the incrementer. $n will always be the value of the array's index, which conveniently is sequential numbers starting at 0. so this line:

      echo '<tr><td style="text-align:center;"><b>Image : '.($i + 1).'</b></td></tr>';

should be

      echo '<tr><td style="text-align:center;"><b>Image : '.($n + 1).'</b></td></tr>';

 

As for your HTML questions...i will say again, the html is whatever you want it to be. PHP does not limit what you can/can't use for HTML. So if you want DIV tags around the form, put them around the form.

 

And if you want to control what HTML is printed with a conditional, that is easy too. You can either do it with print statements in PHP, or you can close and reopen the PHP tags:

<?php
  $color = 'red';
  if($color == 'blue'){
    //simple print statement
    print 'I found blue';
    //good for short blocks of text
  }else{
    //Or we can stop PHP
?>
This text will only get printed if the color is not blue.
<form>
  <input>
</form>
<?php
    //And continue it later
  }
?>

Link to comment
Share on other sites

<form method="get"> 
  <select name="gid">
    <option>1</option>
    <option>2</option>
    <option>3</option>
  </select>
  <input type="submit" value="Visit Gallery" />
</form>
<?php
  if(isset($_GET['gid'])){
    $file = "gallery{$_GET['gid']}.txt";
    echo '<table border="2" style="margin:auto;">';
    foreach(file($file) as $n => $line){
      echo '<tr><td style="text-align:center;"><b>Image : '.($i + 1).'</b></td></tr>';
      echo '<tr><td style="text-align:center;"><img src="'.$line.'"></td></tr>';
    }
    echo "</table>";
  }
?>

 

In regards to the above code , can you tell me what the following lines stand for ?

 

$file = "gallery{$_GET['gid']}.txt";

    echo '<table border="2" style="margin:auto;">';

    foreach(file($file) as $n => $line){

 

How come there is no fopen() statement at all ?  And what is this file($file) ?

Link to comment
Share on other sites

Read up on file() here: http://us2.php.net/file

Long story short, it returns all the lines of a file as an array. Very convenient if you are writing a script that needs to process each line as an entry.

 

How familiar are you with HTML? If you know how to do a title in HTML, then making a dynamic one is super simple. Let's wrap the code we have so far with all the proper HTML stuff:

<html>
  <head>
    <title>This is my title</title>
  </head>
  <body>
    <form method="get"> 
      <select name="gid">
        <option>1</option>
        <option>2</option>
        <option>3</option>
      </select>
      <input type="submit" value="Visit Gallery" />
    </form>
<?php
  if(isset($_GET['gid'])){
    $file = "gallery{$_GET['gid']}.txt";
    echo '<table border="2" style="margin:auto;">';
    foreach(file($file) as $n => $line){
      echo '<tr><td style="text-align:center;"><b>Image : '.($i + 1).'</b></td></tr>';
      echo '<tr><td style="text-align:center;"><img src="'.$line.'"></td></tr>';
    }
    echo "</table>";
  }
?>
  </body>
</html>

 

Now, if we want to make the title dynamic, we can just throw some PHP code in there:

<html>
  <head>
    <title>Gallery <?php echo $_GET['gid']; ?></title>
  </head>
......

Link to comment
Share on other sites

Oh my god ! Thanks very much ! I am familiar with HTML titles but was wondering whether once you put the <html> tag you can name your file as ".php". I thought that was not possible.Thanks for the tip on dynamic title as well , that was pretty cool !

 

And thanks for the tutorial as well !

 

 

Link to comment
Share on other sites

Also I have replaced the following code

 

echo '<tr><td style="text-align:center;"><b>Image : '.($i + 1).'</b></td></tr>';

 

with

 

echo '<tr><td style="text-align:center;"><b>Image : '.(++$i).'</b><b><a href="#top">top</a></b></td></tr>';

 

and added

 

<a name="top">

 

after the form code.And I believe you can add it before the form code as well.

 

Is that OK ? Syntactically ? Does it introduce any security holes ?

 

[This modification ensures he can navigate to the top of the page instantly from any image he is viewing to access the gallery drop down box.]

 

 

Link to comment
Share on other sites

Now in this gallery if we see when the display_gallery.php file is invoked , it first brings up a drop down box and we have to select a gallery.After we do that the gallery is displayed and so is the drop down menu and we can select another gallery.

 

But the first time , no gallery is displayed.Only the drop down menu is displayed.Now suppose I want a random gallery to be displayed the very first time the user accesses display_gallery.php , how do I do it ?  ???

Link to comment
Share on other sites

Update the PHP code section to be:

<?php
  //We'll use $gid to hold the gallery number
  if(isset($_GET['gid'])) //Gallery is passed
    $gid = $_GET['gid'];
  else //No gallery is passed, pick random
    $gid = rand(1,3); //Pick random number between 1 and 3
  $file = "gallery{$gid}.txt"; //Update to use $gid
  echo '<table border="2" style="margin:auto;">';
  foreach(file($file) as $n => $line){
    echo '<tr><td style="text-align:center;"><b>Image : '.($i + 1).'</b></td></tr>';
    echo '<tr><td style="text-align:center;"><img src="'.$line.'"></td></tr>';
  }
  echo "</table>";
?>

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.