<?php
if ( isset( $_GET['fail']) && !empty($_POST["uname"]))
{
echo '<script language="javascript">';
echo 'alert("Your name or password is incorrect")';
echo '</script>';
}
How do I check for empty field when I press the submit button?
Why do you have a "GET" and a "POST" check in your php code? You can't use both verbs when sending an HTTP request unless your form in html looks something like:
<form id="myForm" method="post" action="myAction.php?fail=someStatus">
<input type="text" name="uname" required />
</form>
If you are not sending the value of fail, the isset( $_GET['fail']) will always return false and your code will never get past your if
<?php
// isset( $_GET['fail']) is always false
if ( isset( $_GET['fail']) && !empty($_POST["uname"]))
{
echo '<script language="javascript">';
echo 'alert("Your name or password is incorrect")';
echo '</script>';
}
You could use required attribute of html5 in your input tag as
<input type="text" name="USERNAME" required>
<input type="text" name="PASSWORD" required>
This won't allow to submit form without content in the textbox.
I don't see what you are trying to do here. If you submit your form with Ajax you can request a php script (eg. form_submit.php) where you validate your form fields and return the response which will be used by your javascript to display something if there is an error (or not).
Here is simple process to explain how you can do:
html form submitted => Javascript with Ajax call to form_submit.php => Form verification with PHP => Return result from verification => Javascript Ajax get the response and do what you want to do like displaying an error/success popin for example.
Related
I have configured the javascript code to do auto submit but what I want is that if the authentication fails, I do not do the autosubmit again.
My code is the following:
Form:
<?php echo form_open($this->uri->uri_string(), array('class' => 'login-form')); ?>
<div class="form-group">
<label for="email"><?php echo _l('clients_login_email'); ?></label>
<input type="text" autofocus="true" class="form-control" name="email" id="email">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<label for="password"><?php echo _l('clients_login_password'); ?></label>
<input type="password" class="form-control" name="password" id="password">
<?php echo form_error('password'); ?>
</div>
<?php echo form_close(); ?>
Javascript
<script type="text/javascript">
window.onload=function(){
var auto = setTimeout(function(){ autoRefresh(); }, 100);
function submitform(){
document.forms["login-form"].submit();
}
function autoRefresh(){
clearTimeout(auto);
auto = setTimeout(function(){ submitform(); autoRefresh(); }, 10000);
}
}
</script>
How can I do it?
I believe your problem lies in array('class' => 'login-form').
Looking at the documentation for document.forms, you access the forms with ID.
I am not familiar with code-igniter; however, your code tells me that you are probably setting the class for a form. You would need to set the id for a form.
For preventing the auto-submit from running twice. From what I see, I suspect that you are getting a normal HTML form at the end. When you submit a HTML form it would make a trip to the server and when it comes back it should reload the page (unless you make it an asynchronous form).
Since the page is reloading, the window.onload would be run every time. To prevent this you would have to use a true-false flag some wheres.
Here are some potential solutions:
You could try looking into accessing url parameters from JavaScript. Some quick searching shows that this is a lot more complex than I'd expect though...
Alternatively, you could move the JavaScript code into a <script> block and echo out a script block from PHP. If you are going with this option you should look into using addEventListener method instead of accessing onload directly. PHP would make it much easier to accessing URL parameters by using $_GET or $_POST.
A second alternative would be to echo out a <input type="hidden"> that holds the true/false value and then access the value using JavaScript.
Once you can use your flag, you just need to check the flag in order to decide whether or not to auto-submit or not.
if (myFlag){
//submit form
}else{
//don't submit form
}
This true-false value is not sensitive data so it should be safe to place it as a GET parameter.
Basically I've got a form with 5 radio buttons. No submit button. I want the form to run when the user clicks on any of the radio buttons, so this is what I did.
<input id="5" type="radio" name="star" onchange="this.form.submit();" <?php if ($row["star"] =="5") echo "checked";?> value="5"/>
a querystring is required for the form so I'm using a form action like this
<form name="submit" action="http://example.com/page.php?id=<?php echo $urlid;?>&title=<?php echo $title;?>" method="POST">
and my php is
if ($_POST["submit"]) {
$rating = $_POST['star'];
$title = $_GET['title'];
$verification = ($_GET['verification']);
} else {
//run the page like usual
}
After testing, I found that onclick, it runs the form action, but on the php side, it goes to "else" where is runs the page like usual instead. Any ideas?
Your PHP is checking if $_POST['submit'] contains a value. Your form does not contain a form element with the attribute name="submit", so therefore it fails and moves straight to the else statement.
If you want to check if the form was posted then you should instead check for:
if (!empty($_POST)) {}
or
if ($_SERVER['REQUEST_METHOD'] == 'POST') {}
The form element seems to have invalid attributes, missing a quote and space.
It's generally easier to write a little more code, and keep it clearer
<?php
$url = "http://example.com/page.php?id=". $urlid ."&title=". $title;
?>
<form name="submit" action="<?php echo $url; ?>" method="POST">
Since you are checking with -
if ($_POST["submit"]) { // this checks whether there is any item named 'submit' inside the POST or not
} else {
//run the page like usual
}
The easiest would be to put a hidden item with name submit so that the check validates to true-
<form .. >
....
<input type='hidden' name='submit' value='submit' />
</form>
How can I keep the value of textboxes after submitting a form?
When I press the submit button the values dissapear.
What I want is that the values don't dissapear from the textbox after pressing submit button.
in PHP you can do
<input type="text" name="fieldName" value="<?php echo isset($_POST['fieldName']) ? $_POST['fieldName'] : '' ?>" />
since HTTP is a stateless protocol so it lost previous data from the submitted form.
You can use session or you can do it like this:
<input name="email" value="<?php isset($_REQUEST['email']) ? $_REQUEST['email'] : '' ?>">
it will check if value exists in the super global $_REQUEST if exists then echoing it as value of the input field. set like this for all input field.
I've got a working PHP form that I've built and it contains 5 fields which are pre-populated by a URL query string.
The query string URL is on a hyperlink on an HTML email asking the recipient to click here to make a booking. I've got their details already because I've sent them the email, hence why the form get pre-populated.
How can I get the form to instantly submit on page load and redirect to the thank you page?
Here's how my form looks at the moment:-
<form name="frm" id="frm" method="POST">
<input name="firstname" id="firstname" type="text" value="<?php echo ((isset($_GET["firstname"]))?htmlspecialchars($_GET["firstname"]):""); ?>" />
<input name="lastname" id="lastname" type="text" value="<?php echo ((isset($_GET["lastname"]))?htmlspecialchars($_GET["lastname"]):""); ?>" />
<input name="email" id="email" type="text" value="<?php echo ((isset($_GET["email"]))?htmlspecialchars($_GET["email"]):""); ?>" />
<input name="company" id="company" type="text" value="<?php echo ((isset($_GET["company"]))?htmlspecialchars($_GET["company"]):""); ?>" />
<input name="contactid" id="contactid" type="text" value="<?php echo ((isset($_GET["contactid"]))?htmlspecialchars($_GET["contactid"]):""); ?>" />
<input type="submit" name="Submit" value="submit" />
</form>
And here's the JavaScript I've tried (but didn't work):-
window.onload = function() {
setInterval(function(){
document.getElementById("frm").Submit();
},5000);
}
Here's a pastebin of the whole page: http://pastie.org/private/lgpealjya8xrwqi78gropw
Thanks
Alright so basicaly you have been trying to get information through url, insert them in a form and then submit the form so you can send an email. This is all quite nice although you're just making things harder than they are.
You you need to do is wipe off the form (we don't need this really) and instead of processing all those $_POST information you wanted to get off the form, you gonna do that with the $_GET array you already got off the url, at the end of the day it's exactly the same since the informations you're inserting in the form are only the ones you're getting off the url.
So if you change this the following way, youe script will work just fine :
//Get the submitted data
$firstname = cleanText($_GET['firstname']);
$lastname = cleanText($_GET['lastname']);
$email = cleanText($_GET['email']);
$company = cleanText($_GET['company']);
$contactid = cleanText($_POST['contactid']);
So here is what's gonna happen : the user is going to clic on a link, your script is going to get the $_GET array, process it, send the email based on this array, and this send the user to the thankyou.php page. Note that you could get your script to be more efficient and save the user some time if you insert your script in the thankyou.php.
Last but not least I would like to point out to you that it's not very good practice to get an email sent on url submission... you could end up having a bot send massive amounts of emails through this url and get you email blacklisted. If I were you I'd either control the ip and make sure only 1 mail is sent every 10 minutes or whatever time suits your needs best, or i'd add a captcha to the page so the user would need to show he's a human being who actualy wants to waste some time sending email.
try using: document.getElementById('frm').submit(); instead of document.getElementById('frm1').submit();
and also make it execute once the page has been loaded:
window.onload=function(){
document.getElementById('frm').submit();
}
I have a registration.php page and another registration_checker_page.php.
If username and / or email is taken after they submit, then it'll redirect them back to the previous (registration) page.
But then their filled in data is lost. Is there a way to redirect them without clearing their data?
if(mysql_num_rows($queryUser) > 0){
echo '<p><div id="redirect"/></p>';
}
function redir {
code ... location.href = "registration.php";
}
Edit: By filled data it is first name, username, postcode etc. But other information like password etc will be removed.
You can use session or cookie for same
if(mysql_num_rows($queryUser) > 0){
echo '<p><div id="redirect"/></p>';
}
function redir() {
$userName = $_POST['username'];
$email = $_POST['email'];
setcookie("userName", $userName, time()+3600); /* expire in 1 hour */
setcookie("emial", $email, time()+3600);
code ... location.href = "registration.php";
}
And at your form
<input type="text" name="userName" value="<?php echo isset($_COOKIE['userName']) ? $_COOKIE['userName']:'' ?>">
<input type="text" name="email" value="<?php echo isset($_COOKIE['email']) ? $_COOKIE['email']:'' ?>">
You can above same with using session.
Hope this will help
The best way to solve this, in MNSHO[1], is to make sure those values are never lost in the first place.
Lets assume, for the same of discussion, that you have a field in your HTML called 'username'
If you are using a GET (or a regular redirect), you can include username as part of the
querystring, so it is available on the registration page.
window.location.href = "registration.php?username=" . $_REQUEST(username);
And then, on your registration page:
<input name="username" type="text">
What you'd want to do is look and see if you have an existing value that came in.
<?php
$usernameValue = "";
if (isset($_REQUEST['username'])) {
$usernameValue = $_REQUEST['username'];
}
// Typing on the fly, I may have the type of quotation marks
// flipped around. N
echo '<input name="username" type="text" value="$usernameValue">';
// or this...
echo "<input name='username' type='text' value='$usernameValue'>";
?>
Now, if you are redirecting to this page from another...
[1] My Not-So-Humble Opinion
You could save them a get variable and append that to the file you are referring them too, or do it server side and save them by a temporary ID.
First possibility:
location.href="registraion.php?e=thisdudes#ema.il&u=thisdude"
Use AJAX to check for duplicate username/email immediately after the textbox loses focus and notify the user accordingly. That is how modern pages do it.
Is there a reason why you are waiting for the user to fill the complete form before checking for duplicate username/email?