How To Show/hide Elements Using Php And How To Send User To Another Page
Solution 1:
You can think of using the javascript solution. first style the div to be display:none;
then you can use:
document.getElementsByClassName('error')[0].style.display = "block";
when you wanna make it visible. Also JQuery Answer : (maybe incorrect, friends help plz!)
$('.error')[0].hide();
$('.error')[0].show();
You can also set speed/fade it using instructions here http://www.w3schools.com/jquery/jquery_hide_show.asp
Also for redirecting, yes you can replace them. Alternatively you can use JS solution too. replace
echo '<p><em>Thank you.</em></p>';
with:
echo '<p><em>Thank you.</em></p><script>location.replace("WHERE YOU WANNA GO!");</script>';
Hope this helps.
Solution 2:
First, you need to understand how the server processes PHP and HTML files. The server configuration tells it how to interpret files; some files are served as-is, some files are passed to a handler that executes them as a script, etc. In your server config, there will probably be a line that instructs the server to treat files with the extension .php
as scripts to be executed using PHP. HTML files do not need to be executed; they are sent to the client, and the client's browser interprets the html, css, and js information to render a web page. Because HTML files are not executed as scripts, if you put code (perl, php, java, c++, smalltalk... whatever you want!) in the file, it will not be executed by the server, and it will not be executed by the browser unless the browser has an interpreter for that language, as is the case with HTML, CSS, and javascript. That is why the PHP in your HTML file is not being executed: unless you instruct your web server to interpret that file as a PHP script, it will treat it like a standard html file.
In your case, you want to present an error message to the user if the form validation fails. You also want to keep your code and your HTML file separate (which is a good habit to get into, IMHO!). This presents a problem because you really need to alter the content of the HTML page to include an error message, but the HTML page is essentially static. You could redirect users to another page with the same content, but an error message at the top of the page, but that's a rather ugly solution. You could also use javascript to do the form validation and present the user with an error message if their form submission is invalid, but anyone who has JS turned off will be able to bypass that check.
The most flexible solution for your problem is to use a template system that allows you to customise your page content but keep your PHP code free from HTML. There are a number of PHP template systems out there; google will turn up plenty for you. The templates contain variables that you can set in your PHP script and use in the template; the template engine then parses the templates, interpolates the variables, and outputs the result.
In your case, you could set up your form to display an error message at the top of the form and perhaps to highlight the fields that have been incorrectly filled.
Here is an example using Smarty, a popular, flexible template system:
include('Smarty.class.php');
// create object
$smarty = new Smarty;
if (! empty($_POST)) {
// validate your form input
...
// Minimal form validation:
if (!empty($scrubbed['name']) && !empty($scrubbed['email']) && !empty($scrubbed['comments']) ) {
// send your email
// redirect user to a thank you page
header("Location: thankyou.html");
}
else {
// sets a variable $error in the template
$smarty->assign('error', 'Please fill out the form completely');
}
}
// display it
$smarty->display('form.tpl');
The template file, form.tpl
, should contain your standard form plus any sections that you want added in case of error. Here's a snippet from an example form.tpl
file:
<h2>Contact me!</h2>
{if isset($error)}
<div class="error">
<p>
{$error}
</p>
</div>
{/if}
<form id="contactForm" action="contact.php" method="post">
(etc.)
output if contact.php detects an error:
<h2>Contact me!</h2>
<div class="error">
<p>
Please fill out the form completely
</p>
</p>
<form id="contactForm" action="contact.php" method="post">
(etc.)
output if there is no error (i.e. the form has not been filled in):
<h2>Contact me!</h2>
<form id="contactForm" action="contact.php" method="post">
(etc.)
This is a very basic example of what templates can do. I hope that has clarified things a little and given you a glimpse into the wonderful world of HTML templates.
Solution 3:
Firstly. You need to turn contact.html into an .php file aswell in order to use php code.
Consider naming your contact.php > contact.script.php, and rename contact.html to contact.php.
Then, in your php code you could do something like this:
contact.script.php
if((!empty($scrubbed['name']) && !empty($scrubbed['email']) && !empty($scrubbed['comments']) ) {
...
$message = 'Thank you.';
} else {
# put your error message into the same variable
$message = 'Please fill out the form completely.';
}
contact.php:
/* at the very beginning of your code */
include 'path/to/contact.script.php';
/* Then, wherever you would like to show the message just add this at the end: */
<?=$message?> // this will echo out the message (same as <?php echo $message; ?> - as in:
<div class="error"><p><?=$message?></p></div>
Solution 4:
<input type="text" value="<?php echo $ta;?>">
<div id="content" style="display:<?php echo $ta==2 ? 'block':'none' ?>"></div>
Post a Comment for "How To Show/hide Elements Using Php And How To Send User To Another Page"