|
-
Apr 22nd, 2009, 12:32 PM
#1
Set a text in a HTML dynamically
As you can see from this Contact Form, I would want it that I will be able to place a text above the form in cases when the email and captcha is left blank or when the captcha is wrong, can I do it without using another HTML file?
Following the sample here it looks possible with
PHP Code:
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
but I would like to be able to reset it, that is remove that div everytime the form is posted.
TIA
-
Apr 22nd, 2009, 02:28 PM
#2
Re: Set a text in a HTML dynamically
the problem is that you're using the referer to redirect. if the referer already has the query string set, then you're adding more values to the query string (some of which may not need to be passed). just redirect to the page instead:
PHP Code:
header("Location: contact.html?subject={$subject}&from={$from}&message={$message}&wrong_code=true");
or if you need to do it dynamically, use parse_url():
PHP Code:
//takes "/[PATH]" from http://domain.com/[PATH]?querystring $path = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH); header("Location: {$path}?subject={$subject}&from={$from}&message={$message}&wrong_code=true");
-
Apr 23rd, 2009, 02:33 AM
#3
Re: Set a text in a HTML dynamically
Okay thanks, I will try those, I am totally new to PHP so pls. be gentle with me. =)
Another thing, I want to put the text above the form, is this the correct way to do this?
PHP Code:
<?php if(isset($_GET['empty_email'])){?>
Please specify email.<br />
<?php ;}?>
<?php if(isset($_GET['empty_verification'])){?>
Please specify verification code.<br />
<?php ;}?>
<?php if(isset($_GET['wrong_code'])){?>
Wrong verification code.<br />
<?php ;}?>
<?php if(isset($_GET['success'])){?>
Comment successfully sent.<br />
<?php ;}?>
-
Apr 23rd, 2009, 12:57 PM
#4
Re: Set a text in a HTML dynamically
Another thing, how can I make it such that when the Submit button is pressed and an email address is already specified and yet the verification image text is incorrect then the email address already specified will be retained? Right now upon pressing the Submit button the email address is being blanked out. TIA.
-
Apr 23rd, 2009, 05:28 PM
#5
Re: Set a text in a HTML dynamically
I'm not even sure if those lines would parse; you have a random semicolon there. get rid of it:
PHP Code:
<?php if(isset($_GET['success'])){ ?> Comment successfully sent.<br /> <?php } ?>
and the only way for you to "store" that information is to send it back. don't send variables like "empty_email" or "empty_verification." send variables like "email" (containing the actual value of the email) and then check if it's empty, and the same for verification. you could keep "wrong_code," I guess, unless you wanted to check the value of verification against the cookie you're using. either way -- you already know that SOMETHING is empty because the user has been sent back to this form (because all of those GET variables are set). I hope that makes sense. basically, if you're using a header to redirect the user back to the original form, just store the "old" information in the query string (I assumed you were doing this already because of the looks of your earlier post).
in general though, I think you're just going about this wrong. forms are supposed to be simple, and this script you're using is making it complicated. is there any reason you need to use this script, or did you just get it from a tutorial?
-
Apr 23rd, 2009, 05:35 PM
#6
Re: Set a text in a HTML dynamically
I just got those from the link I provided in my first post. What is the correct way to do this then? I am really new at this and I just copy pasted from the link in the tutorial in my first post.
Last edited by dee-u; Apr 23rd, 2009 at 05:39 PM.
-
Apr 24th, 2009, 05:20 AM
#7
Re: Set a text in a HTML dynamically
how I build forms:
PHP Code:
<?php $revalue = array(); $errors = array();
//has the form been posted? if($_SERVER['REQUEST_METHOD'] == "POST"){ //check for empties foreach($_POST as $key => $value){ if(empty($value)) $errors[$key] = "was empty";
//we only want to check custom errors if there are no current errors. this way, we won't display a huge list if the form is submitted blank. if(!count($errors)){ //do your custom error checks here, like so:
//checks if an @ symbol exists in email key if($key == "email" && strpos($value, "@") == false) $errors[$key] = "is an invalid address";
}
//this is to display our values again $revalue[$key] = htmlentities($value); } //if we have no errors, continue if(!count($errors)){ //process your form. /* mail( .. whatever .. ); */ } }
if(!count($errors) && $_SERVER['REQUEST_METHOD'] == "POST"): //our form was submitted and no errors were found. //display "success" ?> <h1>Success!</h1> <blockquote>you have successfully done whatever it is you were trying to do!</blockquote> <?php else: //show our form, there were either errors or it hasn't been submitted! ?> <h1>do something!</h1> <?php if(count($errors)): ?> <blockquote> <p>There were some errors while submitting the form.</p> <ul> <?php foreach($errors as $key => $reason): ?> <li><strong><?php echo $key; ?></strong> <?php echo $reason; ?></li> <?php endforeach; ?> </ul> </blockquote> <?php endif; ?> <form action="thispage.php" method="post"> name: <input type="text" name="name" value="<?php echo $revalue['name']; ?>" /><br /> email: <input type="text" name="email" value="<?php echo $revalue['email']; ?>" /><br /> <input type="submit" value="Submit" /> </form> <?php endif; ?>
so, I hope the comments are at least somewhat helpful. basically, the form is submitted to itself, and it then checks all of the POST variables to see if any are empty. you can also make it check other things, but it's just a rough example that I typed out. it checks if the "email" field has an @ symbol in it (though this is a terrible way of legitimately checking if an email is correct!), and produces an error if not. then, all of the errors are listed later on. the previous values are also stored in $revalue so that they can easily be displayed in the <input> fields later on. if no errors were found, the form will be submitted (and it will mail you, or do whatever). after that, you're finished actually processing the form.
then, the script decides whether to show a "success" page, or if the form needs to be shown. we apply logic; if the $errors array is empty and the form was submitted, then there must be no errors, meaning a success page should be shown. if this criteria is not met, then either there are errors or the form hasn't been submitted at all, so we show the form.
this may not be the best way of doing things, but I've been processing forms with methods similar to this for a long time now! it's fairly simple.
you could also make an array of field names/etc to dynamically create your form. this probably wouldn't help much unless you were making a lot of forms or something, though.
finally, if the code above is confusing to you, then you should always ask questions. it may be a lot to swallow.
Last edited by kows; Apr 24th, 2009 at 05:24 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|