views:

89

answers:

3

I'm new to PHP. Iv created a small php script. Basically I have a form and inside of it I have a function called show_sent:

        <form method="post" action="contact.php" class="formstyle">
            <h2>Formulaire de contact : </h2>
            <p>&nbsp;</p>

    <?
function show_sent(){
    ?>
    <p>Sent</p>

            <?
} // the function finishes here
?>

.......

I was hoping that it would only show the 'Sent' text when I call that function. How could I do this? Thanks

contact.php is the same page as the form

A: 

You need to check to see if the form data is posted. You do this by going:

if(isset($_POST['form_element_name']))
{
    //call the show_sent function because data has been posted
    show_sent();
}

or

function show_sent(){
    if(isset($_POST['form_element_name']))
    {

    }
}
//Call the show_sent function all the time because the code inside the function checks the POST variables.
show_sent();
Sam152
A: 

One way to do it is post the form to its self instead of another file then you can check if the variables have data in them and if they do call your sent function. check out this link for more info on post self. http://www.webmaster-talk.com/php-forum/51903-php-self-submitting-form.html

Zen_silence
+4  A: 

You need to clean up your code a bit. Jumping into and out of HTML and PHP is not a good thing.

<?php

  function show_sent() {
    print "<p>Sent</p>";
  }

  if ($_POST) {
    $errors = false;
    /* logic to check data */
    if (!$errors)
      show_sent();
  }

?>

  <form>
    <input type="text" name="fname" />
  </form>
Jonathan Sampson