Help me code in php: very urgent

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
om.bitsian
Forum Commoner
Posts: 36
Joined: Wed Sep 09, 2009 4:13 am

Help me code in php: very urgent

Post by om.bitsian »

Hello frinds,
i have a form in which 30 fields are there. i. e name, age, date of birth....
problem 1) when i click on submit button and if there is an error in 20th field ...the form will clear all the values and agian i have to enter all 30 fields.
for this i am using value="<?php if (isset($_POST['submit'])) echo $_POST['fnam']; in input tag like
<label>4(a). First Name</label>
<input type="text" name="fnam" size="10" value="<?php if (isset($_POST['submit'])) echo $_POST['fnam']; ?>"><br><br>


but the problem here is when all the entries are correct and data is submitted then also the first name and others value will be remain in the same page.........the form will not clear out.
?????? anybody know the alternate way to have the fileds on the form when i refresh or submit the form

problem 2)
do anynbody know how to clear form when i am using value="<?php if (isset($_POST['submit'])) echo $_POST['fnam']; on every input to store the value on the form.


problem 3) i am having some select list say code list.....suppose it contains 3 values (1,2,3).........when i click on submit button and there is a error in some field or when refresh the form is resetting the its value
?????? can anybody tell me how to be with the same option (say option 3) when i do the above operation

thank you
SimonMayer
Forum Commoner
Posts: 32
Joined: Wed Sep 09, 2009 6:40 pm

Re: Help me code in php: very urgent

Post by SimonMayer »

om.bitsian wrote: but the problem here is when all the entries are correct and data is submitted then also the first name and others value will be remain in the same page.........the form will not clear out.
?????? anybody know the alternate way to have the fileds on the form when i refresh or submit the form
u
The way to do this is to force the page to redirect to itself after you have done what you need to do, but before creating the HTML.

Use:

Code: Select all

header("Location: index.php");
where index.php is the page that contains the form

I'm afraid I'm not able to look into the other problems you have at present but

Code: Select all

value="<?php if (isset($_POST['submit'])) echo $_POST['fnam'];
looks like it should have curly braces, like so

Code: Select all

value="<?php if (isset($_POST['submit'])){ echo $_POST['fnam']; } ?>"
But you could also just do

Code: Select all

value="<? echo $_POST['fnam']; ?>"
because when the variable is blank (pre-submission), it will just appear blank in the HTML.
om.bitsian
Forum Commoner
Posts: 36
Joined: Wed Sep 09, 2009 4:13 am

Re: Help me code in php: very urgent

Post by om.bitsian »

no what i just want is when all entries are correct and i press submit button it should clear the form showing messge "Data entered successfully. The PSRN generated is ",$psrn." " but the form is not clearing the fields....

suppose if i remove value="<?php if (isset($_POST['submit'])) echo $_POST['fnam']; ?>" from the input value first name than every time i have to enter first name and other values if there is a error in any field of the form.......in my form around 30 entries are there so its difficult to write all entries again and again ....because i am testing all my fields(validation)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Help me code in php: very urgent

Post by onion2k »

The proper way of doing things is not to echo anything from $_POST or $_GET. You should be putting the value into a different variable that you work with and echo back to the form... eg

Code: Select all

$f_name = htmlentities($_POST['name']);
 
if (empty($f_name)) { $error = "Name was missing."; }
 
echo "<input type=\"text\" value=\"".$f_name."\">";
 
If you were doing that then you could do unset the value if the form completes successfully. eg

Code: Select all

if (!empty($psrn)) {
  $f_name = '';
}
SimonMayer
Forum Commoner
Posts: 32
Joined: Wed Sep 09, 2009 6:40 pm

Re: Help me code in php: very urgent

Post by SimonMayer »

om.bitsian wrote:no what i just want is when all entries are correct and i press submit button it should clear the form showing messge "Data entered successfully. The PSRN generated is ",$psrn." " but the form is not clearing the fields....

suppose if i remove value="<?php if (isset($_POST['submit'])) echo $_POST['fnam']; ?>" from the input value first name than every time i have to enter first name and other values if there is a error in any field of the form.......in my form around 30 entries are there so its difficult to write all entries again and again ....because i am testing all my fields(validation)
Using header("Locate: "); will clear the form down, as it will redirect you to the same page and lose the $_POST variables.
To create the message, you would probably be best off using session cookies. See: http://www.php.net/manual/en/function.session-start.php
om.bitsian
Forum Commoner
Posts: 36
Joined: Wed Sep 09, 2009 4:13 am

Re: Help me code in php: very urgent

Post by om.bitsian »

friends i am new in php
can u please explain me how should i use your code ....this is the code i am using for first name
<label>4(a). First Name</label>
<input type="text" name="fnam" size="10" value="<?php if (isset($_POST['submit'])) echo $_POST['fnam']; ?>"><br><br>

then
$fnam=($_POST['fnam']);
if(strlen($fnam)>10 || strlen($mnam)>10 || strlen($lnam)>10)
{
die ("Error:Maximum length of Name exceeded");
}
if(trim($_POST['fnam']) == "")
{
die ("Enter a valid name");
}

if(preg_match('/[^A-Za-z .]/', $_POST['fnam']))
{
die ("Error: Name should be a character value.");
}
User avatar
morris520
Forum Commoner
Posts: 60
Joined: Thu Sep 18, 2008 8:56 pm
Location: Manchester UK

Re: Help me code in php: very urgent

Post by morris520 »

om.bitsian wrote:friends i am new in php
can u please explain me how should i use your code ....this is the code i am using for first name
<label>4(a). First Name</label>
<input type="text" name="fnam" size="10" value="<?php if (isset($_POST['submit'])) echo $_POST['fnam']; ?>"><br><br>

then
$fnam=($_POST['fnam']);
if(strlen($fnam)>10 || strlen($mnam)>10 || strlen($lnam)>10)
{
die ("Error:Maximum length of Name exceeded");
}
if(trim($_POST['fnam']) == "")
{
die ("Enter a valid name");
}

if(preg_match('/[^A-Za-z .]/', $_POST['fnam']))
{
die ("Error: Name should be a character value.");
}


lol mate

please don't cross-post so many same stuffs! please kindly ask questions in this forum.
om.bitsian
Forum Commoner
Posts: 36
Joined: Wed Sep 09, 2009 4:13 am

Re: Help me code in php: very urgent

Post by om.bitsian »

sorry for pasting code........thanks SimonMayer header() concepts is working fine
Post Reply