if (!(isset($_POST['fullname']) && strlen($_POST['fullname']))) {
echo
"<script type=\"text/javascript\">".
"window.alert('You must enter your full name.');".
"</script>";
exit;
}
The above code is in the register.php file. I have html form in the index.html. when i post the form without full name. it displays alert but page gets stuck at register.php (blank page.)
i want to display the alert on index.html page or atleast get redirected to index.html.
how to do it???
Try window.location.href = '/index.html inside script
if (! (isset($_POST['fullname']) && strlen($_POST['fullname']))) {
echo "<script type=\"text/javascript\">window.alert('You must enter your full name.');window.location.href = '/index.html';</script>";
exit;
}
Do it this way:
if (!(isset($_POST['fullname']) && strlen($_POST['fullname']))) {
echo
"<script type=\"text/javascript\">".
"window.alert('You must enter your full name.');".
"top.location = 'index.html';".
"</script>";
exit;
}
Your exit command is stopping execution, that is why it gets stuck at register.php.
If you want to redirect to another page: http://www.cyberciti.biz/faq/php-redirect/
Do 1 thing.. Put this if condition in the same page. and submit your form on the same page.
it's better idea. if you don't need register.php file for different reason.
It can be done by
windows.location("index.php").
but it's now good way.
<?php
if (!(isset($_POST['fullname']) && strlen($_POST['fullname']))) {
echo
"<script type=\"text/javascript\">".
"window.alert('You must enter your full name.');".
'window.location.href="index.html";'.
"</script>";
exit;
}
You'll want to do this completely client side - a trip to the server isn't even necessary in order to determine whether or not the fullname field was filled out.
Try something like the following on your registration page:
<form id="myform" action="register.php" method="post">
Full Name: <input type="text" name="fullname" />
<input type="submit" value="Submit" />
</form>
<script type="text/javascript">
document.getElementById('myform').onsubmit=function(){
if(!this.fullname.value) {
alert("You must enter your full name")
//stop form submission
return false
}
//continue on to register.php
return true
}
</script>
Working JS Fiddle:
http://jsfiddle.net/GAk8C/
When you are posting a form using PHP, it redirects the browser to that page. Why not alert the error message before submitting the form? Here is a short example:
<form name="contactForm" action="register.php">
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" />
<input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
</form>
<script>
$(function() {
$('#contactForm').on('submit', function() {
// validate and process form here
if( !$("#name").val() ) {
alert('You must enter a valid name!');
return false;
}
});
});
</script>
I Think You are doing this for validation of the field.
It is better to do the simple validation in index.html page itself.
use
in your html code in the index.html pge add the folloimg in your submit button
<input type="submit" name="submit" value="yourvalue" onclick="return validateName()">
And use the javascript validation method as
<script language="javascript" >
function validateName(){
if(document.getElementById("idOfFirstNameField").value==""){
alert("Please enter your name");
document.getElementById("idOfFirstNameField").focus();
return false;
}
}
</script>
Better way, you can give javascript on index.html itself. so when user will not put fullname then it will not redirect to register.php. it will stay on same page with alert error message means on index.html
in head of HTML you put like that
<head>
function validate(){
fname=document.getElementByID('fname').value;
if (fname==""){
alert("Please provide full name.");
return false;
}else{
window.location.href = 'register.php';
}
}
</head>
<body>
<form name="adminform" src="register.php" onsubmit="return validate();">
<input type="text" id="fname" name="fname" value=""/>
</form>
</body>
if user put full name then it will redirect to register.php
The way i do it. In register.php file:
<?php session_start();
if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['subject'])) || (empty($_POST['message']))) {
$_SESSION['mensaje']="<script language=\"javascript\">alert('All fields are mandatory')</script>";
header("Location: ../contact.php");
exit;
}
And in the first lines of the file that you will redirect in case of error (in this case contact.php)
<?php session_start();
if(isset($_SESSION['mensaje'])){
echo $_SESSION['mensaje'];
}
session_destroy();?>
<!DOCTYPE html>
Also you can use the same way to show a success message. Hope it helps.
Related
I want a validation onkeyup from an input form, I've made a jquery. I'm having a hard time finding why is it not checking? I need to go to a php page in checking. What could be wrong to my code that it don't work?
<html>
<head>
<title>
reg
</title>
</head>
<body>
<form method="post">
<input type="text" name="name" onkeyup="check_user()" id="name"/><div id="checking">Checking</div>
<button type="submit" name="submit">Submit<button>
</form>
<script src="jquery-3.3.1.min.js"></script>
<script>
document.getElementById("submit").disabled=true;
function check_user(){
var name = document.getElementById("name").value;
$.post("user_check.php",
{
user: name
},
function(data,status){
if(data=='<p style="color:red">Name contains a number</p>'){
document.getElementById("submit").disabled=true;
}
else{
document.getElementById("submit").disabled=false;
}
document.getElementById("checking").innerHTML=data;
}
);
}
</script>
</body>
</html>
user_check.php
<?php
if (isset($_POST['user'])) {
if(preg_match('/^\d+$/', $_POST['user'])) {
echo '<p style="color:red">Name contains a number</p>';
} else {
echo '<p style="color:green"> You can take this name</p>';
}
}
?>
You can validate the text box by adding the pattern attribute like this pattern="[a-zA-Z]+"
<input type="text" name="name" pattern="[a-zA-Z]+" title="please just add strings" id="name"/>
i also notice in your code you did not add the id for the submit button thats why your submit button is not disabled, your code should like this for submit button.
Submit
1- first add the id attribute of the submit button id="submit"like this
<button type="submit" id="submit" name="submit">Submit<button>
2- update the code on user_check.php
if (isset($_POST['user'])) {
if(!preg_match('/^([^0-9]*)$/', $_POST['user'])) {
echo '<p style="color:red">Name contains a number</p>';
} else {
echo '<p style="color:green"> You can take this name</p>';
}
}
My research is pointing me to Ajax and/or JQuery, but I wanted to ask to make sure I understand and move in the right direction.
I've been looking to use a javascript onclick function to create a popup(alert) asking to confirm that I want to proceed and show the changes I'm about to make.
I hoped that I could use PHP $_POST or GET to echo the change in the popup/alert window. However without the page refresh it appears I can't do this, but looking for a confirmation? Should I be looking for Ajax to do this? Any thoughts/suggestions would likely give me a head start as php/ajax is sort of foreign to me.
Sample code:
<script>
function clicked() {
if (confirm('Do you want to submit? <?php if(isset($_POST['city'])){$city = $_POST['city'];echo $city;}?>'))
{
yourformelement.submit();
} else {
return false;
}
}
</script>
</head>
<form action="output.php" method="post">
<input name="city">
<input type="submit" onclick="clicked();" value="Button" />
</form>
</html>
You can get the input value with jQuery before submitting:
<form action="output.php" method="post" id="form">
<input name="city" id="city_input">
<input type="submit" value="Button" />
</form>
<script>
$('#form').submit(function(e) {
e.preventDefault();
let cityInputVal = $('#city_input').val();
if (confirm('Do you want to submit ' + cityInputVal + '?'))
{
this.submit();
} else {
return false;
}
});
</script>
The preventDefault() function stops the form submission and therefore, page refresh.
I think this question has been answered many times. But still i didn't find a suitable solution. i want to display "wrong id/password" message in my admin_login.php page itself when my id and password do not match. how to do it? Please help. Here is my code
admin_login.php
<body>
<script type='text/javascript'>
<?php
if(isset($_SESSION['error_message']))
{
?>
alert("<?php echo $_SESSION['error_message']; ?>");
<?php
unset($_SESSION['error_message']);
}
?>
</script>
<section class="container">
<div class="login">
<h1>Login to Admin Panel</h1>
<form method="post" action="admin_logincode.php" enctype="multipart/form- data">
<p><input type="text" name="uname" id= "uname" value="" placeholder="Username or Email"></p>
<p><input type="password" name="password" id="password" value="" placeholder="Password"></p>
<p class="remember_me">
<label>
<input type="checkbox" name="remember_me" id="remember_me">
Remember me on this computer
</label>
</p>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
</form>
</div>
</section>
</body>
This is my php code: admin_logincode.php
<?php
include ("connection.php");
session_start();
$uname=$_POST['uname'];
$password=$_POST['password'];
if($uname=="admin" && $password=="2015admingoswami")
{
$_SESSION['uname']=$uname;
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + 120;
header("location:admin_home.php");
}
else
{
$_SESSION['error_message']="Wrong Username or Password";
header("location:admin_login.php");
}
?>
I have done the coding part to display the error message but still it didn't work. Please help me out.
Make sure you're not forgetting to call session_start() on the page where you want to print the error.
You also can make an AJAX call (with jQuery) when the submit button of your admin_login.php page is clicked, and the target of the call would be admin_logincode.php. The result of the AJAX call could be appended to any part of your form (a DIV for instance) so you can append here "wrong password" without refreshing the login page. If the login/pass match, then admin_logincode.php will initiate a session.
Just check on your index page if the session exists, almost like as you did.
//if session exists, then user is authentified
if (isset($_SESSION['auth'])) {
//display the user menu, or whatever
}
//else display the login form
else {
display_login();
}
Don't forget to "never trust the client" so you have to be careful when handling data posted by the user. Check special chars, length...and pay attention to fields you use to perform MySQL requests.
I have an issue where I am validating form submission with javascript. The form is prefilled with results from the database as PHP values like this:
<form name="profiledit" action="profile_edited.php" method="POST" >
<h3>Name:</h3>
<input type="text" id="teamname" name="teamname"
value="<?php echo $result['teamname'];?>">
</form>
This is the javascript:
<script type="text/javascript">
function empty() {
tn = document.getElementById("teamname").value;
if (! /^[a-zA-Z0-9]+$/.test(tn)) {
alert("Please enter a valid Team Name");
return false;
}
}
</script>
The submit button is :
onClick="return empty();"
The problem is that is always tells me top "Please enter a valid Team Name" unless I retype the text in the box (that was supplied by the PHP value).
I cannot see any weird spaces or things in "view source".
What could the problem be?
Thanks.
EDIT1 : Sorry I forgot to paste closing brace. It was there in the code and this does work for BLANK forms OK. Just not when it has a prefilled value from PHP.
Try this
Check this link
Html
<form name="profiledit" action="" method="POST" id="profiledit">
<h3>Name:</h3>
<input type="text" id="teamname" name="teamname" value=""/>
<input type="button" id="submit" value="submit" name="submit" />
<input type="submit" id="submitform" value="submitform" name="submit" style="display:none;" />
</form>
Jquery
$('#submit').click(function(){
var pattern=/^[a-zA-Z0-9]*$/;
var tn = $("#teamname").val();
if(tn == "" && pattern.test(tn)){
alert('1');
}else {
//alert('2');
$('#submitform').trigger('click');
}
});
Hope its helps
Well, there was nothing wrong with the responses after all. My code was good and you guys code was good as well.
The problem?
Well I just happened to be testing with a teamname that had a SPACE in it!!!!!!!!!!
So having finally worked out that was the problem all along I would like to thank you all for your inputs.
I have used the regex instead : /^\w+( \w+)*$/
Allows words, numbers and a space.
I wish to check if at least one checkbox checked before submission of a form, here is my code:
<script type="text/javascript">
function validate() {
if (document.getElementById('checking').checked) {
alert("checked");
} else {
alert("You didn't check it! Let me check it for you.")
}
}
</script>
<?php foreach (..... ) : ?>
<form name="frm1" id="frm1" method="post" action="next.php" >
...
<input type="checkbox" value="" class="chk" id="checking"/>
...
....
</form>
I did like that but it seems dosn't work for me,I see the message but at the same time it make submission also that not enought to verify atleast one checkbox is checked?
Onclick is firing both events, the validation and the submit. You could put the submitting part inside the validation script for instance and only submit the form, if your validation was successful.
EDIT: posting a more complete code sample
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
function validate() {
if ($('#checking').is(':checked')) {
$('#frm1').submit();
} else {
alert("You didn't check it! Let me check it for you.");
$('#checking').attr('checked', true);
}
}
</script>
<form name="frm1" id="frm1" method="post" action="next.php" >
<input type="checkbox" value="" class="chk" id="checking"/>
<img src="/submit.png">
</form>
</body>
</html>
Also, you should consider also adding server side validation, if none exist yet, since a user can easily change the HTML/JavaScript on your page on runtime with tools like FireBug for instance and submit data that's not valid anyway.
I'd suggested in my original answer that you'd need "something like ...".
Here is what should be a full working solution. The trick that needed to be added was that validate should return a true/false so that we can decide whether or not to submit the form.
<script type="text/javascript">
function validate() {
if (document.getElementById('checking').checked) {
alert("checked");
return true;
} else {
alert("You didn't check it! Let me check it for you.")
return false;
}
}
</script>
<form name="frm1" id="frm1" method="post" action="next.php" >
<input type="checkbox" value="" class="chk" id="checking"/>
</form>
Original Answer:
You probably need something in your click handler that is more like
if (validate()) { $(this).closest('form').submit(); }
This should make the submission wait until the alert is closed.
If you want the form to not submit if the checkbox thing didn't happen, then you could return true/false from validate() and the same would work.