having trouble with basic web form w/ php

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

Post Reply
xplore
Forum Newbie
Posts: 16
Joined: Mon Apr 06, 2009 12:06 pm

having trouble with basic web form w/ php

Post by xplore »

I'm just designing a simple form for input from the user for transmission using the mail function. I have no error checking yet because I cannot get the basics even working.

Code: Select all

 
TOP OF PAGE
<?php
$first_run = (isset($_POST["$first_run"])) ? $first_run += 1 : 0;
?>
 
<-- in between code not necessary for help -->
 
middle code
<table>
            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <tr><td>Email : </td><td>
            <input tabindex="0" type="text" name="user_email" /></td></tr>
            <tr><td>Name : </td><td>
            <input type="text" name="user_name" /></td></tr>
            <tr><td>Company(optional) :</td><td>
            <input type="text" name="user_company" /></td></tr>
            <tr><td>Phone Number : </td><td>
            <input type="text" name="user_phone" /></td></tr>
            <tr><td>Additional Information :</td>
            <td>
            <textarea cols="40" rows="10" name="user_message"> </textarea>
            </td></tr>
            <tr><td>
            <input type="hidden" name="first_run" value="<?php echo $first_run; ?>">
            <input align="middle" type="submit" value="confirm" />
            </td></tr>
            </form>
      </table>
            
           
            
        <?php 
        
        
      
        if ($first_run > 0 ) {    
        $email = $_POST['user_email'];
        $name = $_POST['user_name'];
        $company = $_POST['user_company'];
        $phone = $_POST['user_phone'];
        $message = $_POST['user_message'];
     
        echo "Your email is : ' $email '<br />";
        echo "Your name is  : ' $name '<br />";
        echo "Your company is : ' $company '<br />";
        echo "Your phone number is : ' $phone '<br />";
        echo "Your message is : ' <br /><br />$message<br /><br />";
        echo "If the information above is correct please click send."; 
        echo "<form method=\"post\" action=\"sent.php\">";
        echo "<input type=\"submit\" value=\"send\" align=\"middle\" />";
        echo "</form>";
                                      
        print("<br /><br /><br /><p>Click send if the above information is correct.</p>");    
            
                 
            
        echo "<form method=\"post\" action=\"sent.php\">";
        echo "<input type=\"submit\" value=\"send email\" name=\"send\">";
        echo "</form>";
        }
        else {
            /* the page has loaded for first time run so nothing is shown until user clicks confirm */
        }
        ?>


So basically I would like at the first run time of the page that only the table be shown and the confirm button be visible. Then when they click confirm my understand is that PHP_SELF will reload the page causing the conditional ' $first_run = (isset($_POST["$first_run"])) ? $first_run += 1 : 0; ' to increment $first_run + 1 so that my php at the bottom of the page the conditional ' if $first_run > 0 ' will become true so the user can verify that the information they submitted is correct and then the user will be able to click send. Send will call send.php which just uses the mail function and displays that the information has been sent.


When running this code the bottom php never becomes visible after i hit confirm. Can someone just explain the best route to take for mixing in error checking and showing html based on what "stage" the user is at on the page. Maybe a link to a tutorial? Thanks for help!
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: having trouble with basic web form w/ php

Post by temidayo »

Put the form (which is inside the table) inside your else condition like this:

Code: Select all

 
TOP OF PAGE
<?php
$first_run = (isset($_POST["$first_run"])) ? $first_run += 1 : 0;
?>
 
<-- in between code not necessary for help -->
 
           
        <?php
        if ($first_run > 0 ) {    
        $email = $_POST['user_email'];
        $name = $_POST['user_name'];
        $company = $_POST['user_company'];
        $phone = $_POST['user_phone'];
        $message = $_POST['user_message'];
     
        echo "Your email is : ' $email '<br />";
        echo "Your name is  : ' $name '<br />";
        echo "Your company is : ' $company '<br />";
        echo "Your phone number is : ' $phone '<br />";
        echo "Your message is : ' <br /><br />$message<br /><br />";
        echo "If the information above is correct please click send.";
        echo "<form method=\"post\" action=\"sent.php\">";
        echo "<input type=\"submit\" value=\"send\" align=\"middle\" />";
        echo "</form>";
                                     
        print("<br /><br /><br /><p>Click send if the above information is correct.</p>");    
            
                 
           
        echo "<form method=\"post\" action=\"sent.php\">";
        echo "<input type=\"submit\" value=\"send email\" name=\"send\">";
        echo "</form>";
        }
        else {
            /* the page has loaded for first time run so nothing is shown until user clicks confirm */
?>
<table>
            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <tr><td>Email : </td><td>
            <input tabindex="0" type="text" name="user_email" /></td></tr>
            <tr><td>Name : </td><td>
            <input type="text" name="user_name" /></td></tr>
            <tr><td>Company(optional) :</td><td>
            <input type="text" name="user_company" /></td></tr>
            <tr><td>Phone Number : </td><td>
            <input type="text" name="user_phone" /></td></tr>
            <tr><td>Additional Information :</td>
            <td>
            <textarea cols="40" rows="10" name="user_message"> </textarea>
            </td></tr>
            <tr><td>
            <input type="hidden" name="first_run" value="<?php echo $first_run; ?>">
            <input align="middle" type="submit" value="confirm" />
            </td></tr>
            </form>
      </table>
 
<?
        }
        ?>
That will solve the problem of visibility but I doubt if the script will be able to send the mail just yet
Last edited by temidayo on Mon Apr 06, 2009 2:17 pm, edited 1 time in total.
xplore
Forum Newbie
Posts: 16
Joined: Mon Apr 06, 2009 12:06 pm

Re: having trouble with basic web form w/ php

Post by xplore »

Would that not just display two exact forms? The one above then the one in the else statement? And why do you think the mail functionality will not work? Thanks for help thus far.
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: having trouble with basic web form w/ php

Post by temidayo »

I made a little correction to the code as you will observe.
Try that code out first (tweaking syntax if necessary).

The reason why I think the code may not work is because after confirmation, you did
not make provision to re-send the data the user POSTed intially
Post Reply