My contact form:Part 2

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
Chaal
Forum Newbie
Posts: 14
Joined: Mon Sep 08, 2003 10:15 am

My contact form:Part 2

Post by Chaal »

Hey there,

I have got my form showing just fine now but the error printing is not working. If I click submit without entering any information in the fields the form refreshes but does not show the error messages, also if I do fill in all the details the form still refreshes.

Is there anyone who can take a quick butchers at the code?

Here is an URL to the text code:

http://www.tangerine.tk/form.txt

Next time you are in Ely I will buy ya a pint :)

Thanks[/url]
Last edited by Chaal on Wed Sep 10, 2003 10:50 am, edited 1 time in total.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

I didnt look at it all in detail, and from a programming point of view it is messy and a bit unusual....

First of all it looks like you depend on register globals being on, you should use the $_REQUEST['varname'] variable instead, and use stripslashes() for the values if magic_quotes_gpc is on..

Here is a simpler sample, this one is also wildly mixing code and content

Code: Select all

<h1>My stuff</h1>
<?php

  $posted = empty($_REQUEST['posted']) ? false : true;  // Determine if a POST or GET was done
  $err = array();  // No errors to begin with

  if ($posted)
  {
      if (!$name = trim( strip_tags( stripslashes( $_REQUEST['name'] )))) $err[] = 'Name Required';
      if (!$email = trim( strip_tags( stripslashes( $_REQUEST['email'] )))) $err[] = 'Email Required';
  }

  if ($posted && !$err)
  {
      // Success - send mail or whatever
      ?><p><b>Thank you</b></p><?php
  }
  else     // Output the form
  {
      foreach ($err as $errmsg) echo 'Error: '.htmlspecialchars($errmsg)."<br>\n";
      ?>
      <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
      <h2>Bla Bla Form</h2>
      <p>Name:
        <input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>"><br>
      E-Mail:
        <input type="text" name="email" value="<?php echo htmlspecialchars($email); ?>"><br>
        <input type="hidden" name="posted" value="1">
        <input type="submit" name="submit" value="Submit">
      </p>
    </form>
   <?php
  }   // end form output
?>
Chaal
Forum Newbie
Posts: 14
Joined: Mon Sep 08, 2003 10:15 am

Post by Chaal »

Hi Stoker,

Thats great :D Thanks!

Could you tell me how I would add this code

Code: Select all

<? echo $_GET&#1111;'title'] ?>


and concatenate into this.

Code: Select all

<input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>

Thanks again :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Just replace the variable name in the echo -

Code: Select all

<input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>
becomes

Code: Select all

<input type="text" name="name" value="<?php echo htmlspecialchars($_GET['title']); ?>
Mac
Chaal
Forum Newbie
Posts: 14
Joined: Mon Sep 08, 2003 10:15 am

Post by Chaal »

Hey thanks for that :)

Its all a learning curve for me - I will get there (one day) :)

I am also getting confused with using option select drop downs, I am not 100% sure how I can validate so an error shows if they did not select yes or no and just submitted leaving the 'Please Select' untouched:

Code: Select all

<select name="fchildren" size="1" id="fchildren" class="text" tabindex="15">
<option value="Please Select">Please Select
<option value="Yes">Yes
<option value="No">No
</option>
</select>
I wish I had something to offer you guys but as you can see I am of no help :roll:

Thanks for any support.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

Code: Select all

...
<option value="1">Yes</option>
<option value="0">No</option>
...
<?php

  if (empty($_REQUEST['fchildren']))
  {
    // No was selected
    $err[] = 'You cant select NO you son of a Gates';
  }
  else
  {
     // Yes was selected
  }

?>
Chaal
Forum Newbie
Posts: 14
Joined: Mon Sep 08, 2003 10:15 am

Post by Chaal »

Thanks for that Stoker :)

If I dare ask one more question :oops:

How do I make the selected input options remain selected when the page refreshes to show items that were not enetered or selected?

At the moment when the page refreshes to show errors the 'please select' lists go back to 'please select' instead of showing what might have been selected.

Your cheques in the post by the way 8O

Thanks
Post Reply