Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to keep PHP from clearing all fields when it validates.
Old 03-06-2010, 06:41 AM How to keep PHP from clearing all fields when it validates.
Skilled Talker

Posts: 51
Name: N
Trades: 0
Please could you help me here I have this code for my contact page:

Code:
<form action="<?php $_SERVER['PHP_SELF']; ?>"method="post" name="form" id="form">
<div class="divRightContact"><p>
  <?php

if (!isset($_POST['submit'])) {

} else {
$Firstname = $_POST['Firstname'];

$Surname = $_POST['Surname'];

$Email = $_POST['Email'];

$Company = $_POST['Company'];

$phone = ereg_replace('[^0-9]', '', $_POST['Telephone']);

$employee = ($_POST['employee']);

$consultingtype = $_POST['consultingtype'];

$message = $_POST['message'];
 
if (!empty($_POST['Firstname'])) {
} else {
$Firstname = FALSE;
echo "<p><font color='red'>Please enter your first name.</font></p>";
}
if (!empty($_POST['Surname'])) {
} else {
$Surname = FALSE;
echo "<p><font color='red'>Please enter your surname.</font></p>";
}
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)){
 } else { echo "<p><font color='red'>Please enter a valid email address.</font></p>"; 
}  
 if (!empty($_POST['Company'])) {
} else {
$Company = FALSE;
echo "<p><font color='red'>Please enter your company name.</font></p>";
}

if (!filter_var($phone, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/")))) {
echo "<p><font color='red'>Please enter a valid phone number.</font></p>";
} 
if ($employee == "employee"){
 
}else {
echo "<p><font color='red'>Please select an item</font></p>";
}

   if ($consultingtype == "consultingtype") {
}else {
echo "<p><font color='red'>Please select an item</font></p>";
}
if (!empty($_POST['message'])) {
} else {
$message = FALSE;
echo "<p><font color='red'>Please enter a message.</font></p>";
}
  
if(isset($_POST['checkbox'])) {
} else {
echo "<p><font color='red'>You must agree to the Terms and Conditions.</font></p>";
}
}
?>
However when I run the page the validation all works but say if I enter a valid email address and name but not a message then the PHP will clear all the data including the the valid email address and name I will have to enter everything again.

Please could somebody explain to me step by step how I could solve this problem easily.

Thanks
njccbeach9 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-06-2010, 06:42 AM Re: How to keep PHP from clearing all fields when it validates.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
echo the valid data back into the fields as they are displayed.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-06-2010, 09:03 AM Re: How to keep PHP from clearing all fields when it validates.
Skilled Talker

Posts: 51
Name: N
Trades: 0
Ok thanks but how exactly do I this?

Your feedback will be most appreciated.

Thanks
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 03-06-2010, 12:49 PM Re: How to keep PHP from clearing all fields when it validates.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by njccbeach9 View Post
Ok thanks but how exactly do I this?
For example:

PHP Code:
<input type="text" name="Firstname" value="<?php echo $_POST['Firstname']; ?>" />
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-06-2010, 03:14 PM Re: How to keep PHP from clearing all fields when it validates.
Skilled Talker

Posts: 51
Name: N
Trades: 0
Thank you very much for your reply. It all works except for a few issues.

First of all when the page loads it loads up text inside the textbox like:

<br /><b>Notice</b>: Undefined index: Company in <b>../Contact Us/contactus.php</b> on line <b>417</b><br />

However when I clear it and enter the data and submit it all works the way I want.

More of a problem though, the following method does not work for my 2 drop downs and text area.

Anyway, I think we have almost solved this problem. Your help is most appreciated.
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 03-06-2010, 03:25 PM Re: How to keep PHP from clearing all fields when it validates.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
for "dropdowns" you have to set selected="selected" into the appropriate option.

for a textarea the echo goes between the tags eg:
<textarea><?php echo($post['keyname'])</textarea>

For the other problem we need to see your code.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-06-2010, 03:58 PM Re: How to keep PHP from clearing all fields when it validates.
Skilled Talker

Posts: 51
Name: N
Trades: 0
I have just solved the other problem with the undefined variables so don't worry about that. Thanks for your code for the php code for the Textarea, that all works.

Just the drop down lists, Here is my code for the drop down lists as it stands:

HTML Code:
 <tr>
    <td><p>Number of employees</p></td>
    <td>
 
      <select name="employee" id="employee">
        <option value="please_select">Please select</option>
        <option value="employee">1-10</option>
        <option value="employee">10-20</option>
        <option value="employee">20-40</option>
        <option value="employee">40-80</option>
        <option value="employee">80-160</option>
        <option value="employee">160-320</option>
        <option value="employee">320-640</option>
        <option value="employee">Over 1000</option>
      </select></td>
  </tr>
  <tr>
    <td><p>Consulting type</p></td>
    <td>
      <select name="consultingtype" id="consultingtype">
        <option value="please_select" selected>Please select</option>
        <option value="consultingtype">Financial Support</option>
        <option value="consultingtype">Marketing Support</option>
      </select></td>
  </tr>
I have tried adding php to selected under every option but it just does not work. Please could you help, it's almost done.

Thanks for all your guys
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 03-06-2010, 04:00 PM Re: How to keep PHP from clearing all fields when it validates.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by njccbeach9 View Post
<br /><b>Notice</b>: Undefined index: Company in <b>../Contact Us/contactus.php</b> on line <b>417</b><br />
By default notices are not enabled. In a testing environment they can be useful but otherwise you should have your error reporting set to E_ALL & ~E_NOTICE. Alternatively you could check if the index exists before you try to use it:
PHP Code:
<input type="text" name="Firstname" value="<?php echo (isset($_POST['Firstname'])) ? $_POST['Firstname'] : ''?>" />
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-06-2010, 04:06 PM Re: How to keep PHP from clearing all fields when it validates.
Skilled Talker

Posts: 51
Name: N
Trades: 0
Yes, I have done the alternative:

Code:
<table width="508" border="0">
  <tr>
    <td><p>First name</p></td>
    <td><label>
    <input type="text" name="Firstname" id="Firstname" value="<?php if (!isset($_POST['submit'])) {} else { 
    $Firstname=$_POST['Firstname']; echo "$Firstname"; }?>" >
   </label> </td> 
  </tr>
  <tr>
    <td width="164"><p>Surname</p></td>
    <td width="334"><label>
    <input type="text" name="Surname" id="Surname" value="<?php if (!isset($_POST['submit'])) {} else { 
    $Surname=$_POST['Surname']; echo "$Surname"; }?>" >
    </label></td>
  </tr>
  <tr>
    <td><p>Email Address</p></td>
    <td><label>
      <input type="text" name="Email" id="Email" value="<?php if (!isset($_POST['submit'])) {} else { 
    $Email=$_POST['Email']; echo "$Email"; }?>">
    </label></td>
  </tr>
  <tr>
    <td><p>Company name</p></td>
    <td><label>
      <input type="text" name="Company" id="Company" value="<?php if (!isset($_POST['submit'])) {} else { 
    $Company=$_POST['Company']; echo "$Company"; }?>">
    </label></td>
  </tr>
  <tr>
    <td><p>Telephone Number</p></td>
    <td><input type="text" name="Telephone" id="Telephone" value="<?php if (!isset($_POST['submit'])) {} else { 
    $phone=$_POST['Telephone']; echo "$phone"; }?>"></td>
  </tr>
  <tr>
It all works fantastically but I can't get it to work with drop downs. Any suggestions?
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 03-06-2010, 04:57 PM Re: How to keep PHP from clearing all fields when it validates.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
This is how I normally do it:
PHP Code:
<?php
$options 
= array('option1''option2''option3');
?>
Select One: <select name="mySelect">
<?php foreach($options as $option) : ?>
     <option value="<?php echo $option?><?php echo ($_POST['mySelect'] == $option) ? 'selected="selected"' ''?> ><?php echo $option?></option>
<?php endforeach; ?>
</select>
<?php
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-06-2010, 05:30 PM Re: How to keep PHP from clearing all fields when it validates.
Skilled Talker

Posts: 51
Name: N
Trades: 0
OK thanks, however, I have two values set on my original drop down list. employee and please_select. If the value is employee which is a valid item it will go through if not then it will come up with an error. Here is the validation code I used to validate my drop down:

PHP Code:
if ($employee == "employee"){
 
}else {
echo 
"<p><font color='red'>Please select an item</font></p>";

Thanks
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 03-06-2010, 05:36 PM Re: How to keep PHP from clearing all fields when it validates.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
So why is your "number of employees" selector always going to return a value of "employee"?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-07-2010, 05:20 AM Re: How to keep PHP from clearing all fields when it validates.
Skilled Talker

Posts: 51
Name: N
Trades: 0
It is only going to get give a value of employee if the user has selected a valid item. If the user has not selected an item then the value will not be employee and therefore an error will appear.
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 03-07-2010, 05:51 AM Re: How to keep PHP from clearing all fields when it validates.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Yep, BUT even when the users has selected a "valid item" the return value will be "employee" NOT the number of employees that has been selected.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-07-2010, 05:58 AM Re: How to keep PHP from clearing all fields when it validates.
Skilled Talker

Posts: 51
Name: N
Trades: 0
So you're saying that when the message gets sent, it will send me the value of the drop down not the text of the drop down.
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 03-07-2010, 06:41 AM Re: How to keep PHP from clearing all fields when it validates.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
That how it works for ALL form elements, it is the VALUE attribute that is sent with the name attribute as the key.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-07-2010, 07:21 AM Re: How to keep PHP from clearing all fields when it validates.
Skilled Talker

Posts: 51
Name: N
Trades: 0
Ok I have changed the values for both drop downs and have tried to validate them with PHP without success. Please can you help me:

Here is my new php validation for both drop downs that does not work:

PHP Code:
if ($employee =="1-10" or "10-20"or"20-40" or "40-80" or "80-160" or "160-320" or"320-640" or "1000+"){
 
}else {
echo 
"<p><font color='red'>Please select an item</font></p>";
}

   if (
$consultingtype  == "FinanceSupport" or "MarketingSupport") {
}else {
echo 
"<p><font color='red'>Please select an item</font></p>";

I have tried arraying the values but it does not work like:
($consulting type == array("FinanceSupport" , "MarketingSupport"))

Please can you help me
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 03-07-2010, 08:19 AM Re: How to keep PHP from clearing all fields when it validates.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You don't need to validate the actual value returned from a select box because it will be something you put there.
Simply check that is NOT the default value, which you would have as "Select" or something similar.

your IF statement should be
PHP Code:
if($var == "value" or $var == "value2") {
//

Which would be coded as
PHP Code:
if($var == "value" || $var == "value2" || $var == "value3") {
//

__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 03-07-2010 at 08:21 AM..
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-07-2010, 09:12 AM Re: How to keep PHP from clearing all fields when it validates.
Skilled Talker

Posts: 51
Name: N
Trades: 0
Ok thanks for that observation and correction most appreciated.

Now I have changed the code to this:

PHP Code:
if ($employee == "1-10" || $employee == "10-20" || $employee == "20-40" || $employee == "40-80" || $employee == "80-160" || $employee == "160-320" || $employee == "320-640" || $employee == "1000+"){
 
}else {
echo 
"<p><font color='red'>Please select an item</font></p>";
}
 
   if(
$consultingtype == "FinanceSupport" || $consultingtype == "MarketingSupport") { 
 
}else {
echo 
"<p><font color='red'>Please select an item</font></p>";

How do I make it so that when the user submit and selects a valid value from the drop down it keeps the selection selected?

Thanks.
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 03-07-2010, 10:53 AM Re: How to keep PHP from clearing all fields when it validates.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://phpbeginner.net/2009/10/30/ma...sed-form-data/
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to How to keep PHP from clearing all fields when it validates.

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55226 seconds with 12 queries