harishkumar09 Posted May 1, 2008 Share Posted May 1, 2008 Is it possible to have the html form and the php action script in the same php file ? So that when the choice is made in the drop down form menu the same php script is activated ? Link to comment https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/ Share on other sites More sharing options...
SharkBait Posted May 1, 2008 Share Posted May 1, 2008 <?php if(isset($_POST['submit'])) { echo "You've submitted the form"; } ?> <form action="thisscript.php" method="post"> <input type="submit" value="submit" name="submit" /> </form> Like that? Link to comment https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-530982 Share on other sites More sharing options...
rhodesa Posted May 1, 2008 Share Posted May 1, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-530984 Share on other sites More sharing options...
harishkumar09 Posted May 2, 2008 Author Share Posted May 2, 2008 Yes sharkbait. Like that ! Thank You ! <?php if(isset($_POST['submit'])) { echo "You've submitted the form"; } ?> <form action="thisscript.php" method="post"> <input type="submit" value="submit" name="submit" /> </form> Like that? Link to comment https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-531517 Share on other sites More sharing options...
harishkumar09 Posted May 2, 2008 Author Share Posted May 2, 2008 Thanks Rhodesa Link to comment https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-531519 Share on other sites More sharing options...
harishkumar09 Posted May 2, 2008 Author Share Posted May 2, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-531892 Share on other sites More sharing options...
rhodesa Posted May 2, 2008 Share Posted May 2, 2008 That is a PHP error, please post the code and full error message. Link to comment https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-531894 Share on other sites More sharing options...
harishkumar09 Posted May 3, 2008 Author Share Posted May 3, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-532330 Share on other sites More sharing options...
rhodesa Posted May 3, 2008 Share Posted May 3, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-532439 Share on other sites More sharing options...
harishkumar09 Posted May 4, 2008 Author Share Posted May 4, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-532776 Share on other sites More sharing options...
harishkumar09 Posted May 4, 2008 Author Share Posted May 4, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-532780 Share on other sites More sharing options...
rhodesa Posted May 5, 2008 Share Posted May 5, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-533382 Share on other sites More sharing options...
harishkumar09 Posted May 6, 2008 Author Share Posted May 6, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-534149 Share on other sites More sharing options...
harishkumar09 Posted May 6, 2008 Author Share Posted May 6, 2008 And I think in the above code at : echo '<tr><td style="text-align:center;"><b>Image : '.($i + 1).'</b></td></tr>'; ($i + 1) should be ++$i otherwise there is no incrementation beginning from the first image. Link to comment https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-534221 Share on other sites More sharing options...
rhodesa Posted May 6, 2008 Share Posted May 6, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-534290 Share on other sites More sharing options...
harishkumar09 Posted May 9, 2008 Author Share Posted May 9, 2008 <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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-536552 Share on other sites More sharing options...
harishkumar09 Posted May 9, 2008 Author Share Posted May 9, 2008 Also is it possible to have a title for the browser window as well ? And how to do it ? Can it be dynamically altered like the file name so that the title actually displays the gallery number along with the name of the website ? Link to comment https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-536553 Share on other sites More sharing options...
rhodesa Posted May 9, 2008 Share Posted May 9, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-536663 Share on other sites More sharing options...
harishkumar09 Posted May 10, 2008 Author Share Posted May 10, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-537352 Share on other sites More sharing options...
harishkumar09 Posted May 11, 2008 Author Share Posted May 11, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-538091 Share on other sites More sharing options...
rhodesa Posted May 11, 2008 Share Posted May 11, 2008 the code i originally posted should have $n instead of $i... echo '<tr><td style="text-align:center;">Image : '.($n + 1).'</td></tr>'; but $i++ will work too Link to comment https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-538213 Share on other sites More sharing options...
harishkumar09 Posted May 11, 2008 Author Share Posted May 11, 2008 OK thanks ! Link to comment https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-538226 Share on other sites More sharing options...
harishkumar09 Posted May 14, 2008 Author Share Posted May 14, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-540781 Share on other sites More sharing options...
rhodesa Posted May 14, 2008 Share Posted May 14, 2008 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 https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-540809 Share on other sites More sharing options...
harishkumar09 Posted May 15, 2008 Author Share Posted May 15, 2008 Thank you.I just have a question regarding the isset() function.Is it like an event handler or what ? I mean once a program encounters an isset() function does the program execution stop and a wait begins for a key press on a submit or some such button ? Link to comment https://forums.phpfreaks.com/topic/103710-solved-form-and-php-in-the-same-file/#findComment-541505 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.