php include question.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Here is the easiest posted form to echo statement you will get...

Code: Select all

<?php
if (isset($_POST['form_submitted']))
{
  echo $_POST['form_submitted'];
}
?>
<html>
<head></head>
<body>
<form action="<?php echo basename($_SERVER['SCRIPT_FILENAME']); ?>" method="post">
<input type="submit" name="form_submitted" value="Send this form now or you will melt into cheesy goodness" />
</form>
</body>
</html>
Try running it and see what it does. Of course, I could be totally misunderstanding your question, but using an include can be just as easily handled.
include_page.php

Code: Select all

<?php
if (isset($_POST['form_submitted']))
{
  echo $_POST['form_submitted'];
}
?>
main_page.php

Code: Select all

<?php
include 'include_page.php';
?>
<html>
<head></head>
<body>
<form action="<?php echo basename($_SERVER['SCRIPT_FILENAME']); ?>" method="post">
<input type="submit" name="form_submitted" value="Send this form now or you will melt into cheesy goodness" />
</form>
</body>
</html>
Nicole
Forum Newbie
Posts: 22
Joined: Thu Oct 26, 2006 7:47 am

Post by Nicole »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I knew that was going to happen. No one wants to read all that it is too hard to read and looks intimidating. Here's the first one that I am trying to fix without all the colors. OK I am trying it.

Code: Select all

<?php 
 while ($row = mysql_fetch_array($result)) 
//other stuff up here that works fine. That pops up this form. 
{ 
//this following form works fine too and sends to an email, when the email php is listed on a separate page. ( I include that under this so you can see it.) But when I try to include the email form within like this, it won't send to the email when the submit button is pushed. 
  
$mailto = 'emailaddress@whatever.com'; 
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'> 
<input type='text' name='sendingfield' size=15> 
<input type='submit' name='submit' value='submit' /></form>; 
$another = $_POST['sendingfield']; //field that sends to email when submit is pushed' 
$subject = 'Subject' for email; 
$uself = 0; 
$sep = (!isset( $uself ) ¦¦ ($uself == 0))? '\r\n' : '\n' ; //puts fields into email correctly. 
$messageproper = 'Filled field listed in email field: $another\n';  
mail($mailto, $subject, $messageproper, 'From:' . $sep . 'Reply-To:' . $sep); ";}} 
//maybe I need an "if" echo down here if the submit button is pushed. 
 ?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Nicole
Forum Newbie
Posts: 22
Joined: Thu Oct 26, 2006 7:47 am

Post by Nicole »

Everah, you are so onto what I need. I just need a little help getting through it.

Can you please explain to me about this
="<?php echo basename($_SERVER['SCRIPT_FILENAME']); ?>" It isn't liking it.

For instance I am assuming I put the name of the php that sends the email into 'Script filename'
is this incorrect? Please let me know thanks.
Nicole
Forum Newbie
Posts: 22
Joined: Thu Oct 26, 2006 7:47 am

Post by Nicole »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I think I have what I need, maybe you guys can use this.

Code: Select all

<?php
    if($_POST['text']=="")
    {
               echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
               echo "<p><input type=\"text\" name=\"text\"><p><input type=\"submit\" value=\"submit\"></p></form>";
     }
     else
     {
              echo $_POST['text'];
              echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
              echo "<p><input type=\"text\" name=\"text\"><p><input type=\"submit\" value=\"submit\"></p></form>";
  
     }
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Nicole, seriously now, please use the syntax highlighting tags we've provided for posting code.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Nicole wrote:Can you please explain to me about this
="<?php echo basename($_SERVER['SCRIPT_FILENAME']); ?>" It isn't liking it.
SCRIPT_FILENAME is a server variable. It is essentially the entire path and name of the currently in use file. Wrapping the basename() function around it gets just the page name you are on. It is more secure and more reliable than PHP_SELF.

The code you posted:

Code: Select all

<?php
    if($_POST['text']=="")
    {
               echo "<form method="post" action="{$_SERVER['PHP_SELF']}">";
               echo "<p><input type="text" name="text"><p><input type="submit" value="submit"></p></form>";
     }
     else
     {
              echo $_POST['text'];
              echo "<form method="post" action="{$_SERVER['PHP_SELF']}">";
              echo "<p><input type="text" name="text"><p><input type="submit" value="submit"></p></form>";
 
     }
?>
Can be shortened to:

Code: Select all

<?php
    if($_POST['text'] != "")
    {
              echo $_POST['text'];
    }
?>
<form method="post" action="<?php echo basename($_SERVER['SCRIPT_FILENAME']); ?>">
<p><input type="text" name="text"><p><input type="submit" value="submit"></p></form>
Post Reply