How to display validation error near textbox - php

I have this form in index.php
<form action="result.php" method="post">
<input type="text" name="word" class="tbox">
<input type="submit" name="submit" value="Generate Now">
</form>
In result.php i am checking whether the input matches all validation condition and generating the output. Now, if it doesnt match, i ve to throw an error near my text box. Is that possible through php?

Using PHP, you may try like below
<input type="text" name="word" class="tbox"> <?php if(isset($_POST['word']) && $_POST['word'] == ''){ echo 'YOUR ERROR MESSAGE'; } ?>

a few things, one you can just post against the page itself not great to have DB functions and the like in user reachable areas so check out includes and use them to reference the single page.
Then make a simple variable and slide it into your form as such
<? echo $error ?>
and set error to something via the php script imported from result.php, or you may want to learn about sessions and get variables if combining the files via include isn't on the table.
Once its working then say make a div box as such
<? if (isset($error)){?><div class="errorbox"><? echo $error?></div><? }?>

Related

PHP textbox with input and Set Answer, when submitted right answer go to page, when wrong show error

Create form with input field, set answer to specific string "London". When Submitted if any answer other then "London", have code show error in same page (no alter boxes. if correct answer typed, submit load to another page.
Language using php.
Please help new to php
Create two file, one is trial.php and one is newFile.php(You can give any name);
an follow the code from below
trial.php
<form method="post">
<input name="location" />
<input name='submit' type="submit">
</form>
<?php
if(!empty($_POST['submit'])){
$location=$_POST['location'];
if($location=='London'){
header('location:newFile.php');
}else echo 'Locations is not correct';
}
?>
newFile.php
<?php
echo 'Location is london, that\'s why you are here';

PHP form handling

What I'm trying to do is get simple input from the user an print it out using echo within my php tags but not working for some reason. I'm still a beginner to php so if anyone could assist me, that be dope.
Here's my code :
<p>
<form method="GET">
<input type="text" name="firstname">
<input type="submit" value="sub">
</form>
</p>
<?php
echo $_GET['firstname'];
?>
The file is saved with a .php extension and at the moment, I'm running it locally on my computer using apache. The HTML for the form appears but when I click the submit button, it does not echo what was inputted. I know its not going to the code within the php tag but I'm unsure on how to make it go there.
To finish this you will need a action which means this:
<p>
<form method="GET" action="index.php">
<input type="text" name="firstname">
<input type="submit" name="submit" value="sub">
</form>
</p>
Also if you would like to get the result from the form whenever you hit that submit button, you will have to "tell" php that you would like whenever you hit that submit button to print the result. The simpliest way is this:
<?php
if(isset($_GET['submit']))
{
echo $_GET['firstname'];
}
?>
There are two things that you might wanna do:
In the form action write <?php echo $_SERVER['PHP_SELF']; ?>
The PHP_SELF redirect you to the same .php file with the form data
To avoid any exception, use isset function in the last part of your code, which would check if the POST variable exist or not.
<?php
if(isset($_GET['firstname'])){
echo $_GET['firstname'];
}
?>

how to get meta tags with php, but with html form

I got this PHP src:
$tags = get_meta_tags('http://www.autostraddle.com');
echo "CMS is: ";
echo $tags['generator'];
This code is not user friendly; it's not ready for one online tool for my website, because I want create a simple service - CMS detector, and...
$tag = isset($_REQUEST['url']) ? get_meta_tags('url') : '';
<form action="index.php" method="post">
<input type="text" name="url" size="65" value="<?php echo $tag; ?>"/><br />
<input type="submit" value="Go!">
echo $tag['generator'];
I want create one online tool about detecting what CMS is, but I need this PHP script with HTML form because this will be used from users, and not only from me.
They must put any url, and then the script will perform as described to get the meta tag 'generator'; how do I get the meta tag 'generator'? I want only this meta tag.
Well, and how to do this with 'if else'? if there is meta tag generator do this, but if there is not such tag 'generator', then write any 'echo' message, e.g.
CMS is not known.
Well, this is simple PHP script, but I don't know how to create variables and how to get a URL; mayve with cURL?
What you're struggling with is actually getting the user-side input from the POST superglobals. This is something you can try, but there are many ways to implement this. You could also use method="SET" and capture the user parameters from URL of the action="" file.
Notice, in this case, the action="" parameter is empty. That means the PHP script will execute on the same page where the HTML form is. This might not be what you're trying to do, so if you need to redirect, add the PHP code to a separate file and add it to your website and to the action="" parameter.
<form action="" method="POST">
<input type="text" name="url" size="65" placeholder="http://yoururl"/>
<input type="submit" name="submit" value="Go!">
</form>
<?php
if(isset($_POST['submit']))
{
$tags = get_meta_tags($_POST['url']);
if($tags['generator'])
echo "CMS: <b>" . $tags['generator'] . "</b><br>";
else
echo "No CMS info available.<br>";
}
?>
Give this a shot.

How to stop redirecting to the php page when submitting an HTML form

I've got your run of the mill form, with a PHP script to validate and email it off.
<form id="contactForm" action="contact.php" method="post">
<fieldset>
<p>
<label>NAME:</label>
<input name="name" id="name" type="text" />
</p>
<p>
<label>EMAIL:</label>
<input name="email" id="email" type="text" />
</p>
<p>
<label>COMMENTS:</label>
<textarea name="comments" id="comments" rows="5" cols="20" ></textarea>
</p>
<p><input type="submit" value=" " name="submit" id="submit" /></p>
</fieldset>
<p id="error" class="warning">Message</p>
</form>
The problem is, that when I click submit, it takes me off the page I was on (filling out the form) and takes me to a blank white page - contact.php.
Is there any way I can stay on the original contact.html page after clicking submit and just let the emailing happen in the background?
Ajax is your best best. If not, the quick and dirty way would be to put a php block at the top of you page and put all the stuff from your contact.php in an if(isset($_post['data'])) statement.
Brief example
<?php
if(isset($_POST['variable_from_form']))
{
// send mail, insert in database, etc. stuffs
}
?>
Basically, if there is post data, do something, if not, just write the page as normal
Php is doing the job assigned: it is redirecting to the page you asked for in the action="" tag.
If you need to validate an email or login, you should use the same contact.php form to do both: enter user email and validate and/or login or whatever you want to do.
At the begining of the contact.php use php script to receive submitted fields, validate, sanitize and insert or whatever.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$email=$_POST['email'];
$email=filter_var($id_post, FILTER_SANITIZE_EMAIL);
if(empty($email) )
{$error=['email']="enter a valid email"}
else {
$user_email=$email;
}
}
if(empty($errors) && $_POST)
{
//with $user_email defined and sanitized you can do whatever you want: insert, edit, query, email.
}
elseif ($errors || !$_POST)
{
?>
Here, put all the html form, at the end, close the open php.
<?php
}
?>
You could put an iframe on your page that initially loads a blank page within it and have the form's target be the iframe so it will post into the iframe.
Have the iframe large enough so that you can put a simple message like "Your information has been accepted" can be printed by contact.php
Here's a link that tells you how to do it.
How do you post to an iframe?
you might use javascript (Ajax) to communicate with your server, that way it will continue using the same html file.
Another way it is to rename your html file to .php extension and work on this file.
Good luck!

PHP form validation error messages placement

Is it possible to place error messages resulted from form validation inside the form, using PHP/XHTML/CSS only?
You can put error messages anywhere on the site you wanted. It all depends on where in your scripts you place your code to emit it.
One strategy I've seen used a lot in PHP Frameworks when AJAXy submissions are disabled is to have a field to display the error on the page, and then actually populate that field with the data if the page comes back with an error.
Such as:
<label for="field">Label"><input name="field" type="text" />
<?php if($_POST['errors_field']) echo '<p class="error">'.$errors['field'].'</p>'; ?>
This strategy would only show the <p> tag when the page input box has an error. This method of course involves returning a populated array of all errors to the page when it fails validation.
I would make 2 pages one with the form like this. We will call it form.php. Make sure your form method is "post", and you have named your inputs. create a div that will be used for error callback($error). You can place the Error var anywhere you want not just in the form.
<form method="post">
<input type="text" name="text">
<div><?php echo $error ?></div>
<input type="submit" name="submit">
</form>
Next Make another php page like this and include the form.php page at the bottom. set error as empty string first. See if the button is clicked(isset). If the field is equal to a empty string set the error($error). if no error Process the form. Hope this helps.
<?php
$error = '';
if(isset($_POST['submit'])){
if($_POST['text'] == ''){
$error = "Here is your Error inside the form.";
} else {//"Sanitize and Process the Form";
}}
include 'form.php';
?>

Categories