I have a form page with a authController for validation.
Here is the code of the form page:
<div class="main-content">
<h3>log in as student</h3>
<h3>
<!-- SHOW URL MESSAGE HERE -->
</h3>
<form action="../app/controllers/authController.php" class="login_form" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username" id="" class="input_field">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" id="" class="input_field">
</div>
<div class="form-group">
<input type="submit" name="type" id="" value="Log In" class="btn btn-info">
</div>
</form>
</div>
And here is the code form the authController:
if ($_POST['type'] == 'Log In') {
if (! Validator::notEmpty()->validate($_POST['username']) ||
! Validator::notEmpty()->validate($_POST['password'])) {
$err_msg = "One of the fields is empty";
$user->redirect('stu_login.php?type=' . $err_msg);
exit;
}
How do i show the $err_msg that is defined in the authController, on the form page where the <!-- SHOW URL MESSAGE HERE --> is?
Replace
$user->redirect('stu_login.php?type=' . $err_msg);
To
$user->redirect('stu_login.php?msg=' . $err_msg);
And use in stu_login.php like this
<h3>
<!-- ERROR OR OTHER MESSAGE -->
<?php
if(isset($_GET['msg'] != '')
echo $_GET['msg'];
?>
</h3>
Related
Hi I'm doing a login with roles for example if a user is administrator or a seller have to redirect to their repective index but when I clicked in the login button it redirect me to the same login.php page and I don't know why.
Here is my code
login.php
<section class="login-block">
<div class="container">
<div class="row">
<div class="col-md-4 login-sec">
<h2 class="text-center">Iniciar Sesión</h2>
<form class="login-form">
<form action="process_login.php" method="POST">
<div class="form-group">
<label for="exampleInputEmail1" class="text-uppercase">Usuario</label>
<input type="text" class="form-control" name="usuario" placeholder="">
</div>
<div class="form-group">
<label for="exampleInputPassword1" class="text-uppercase">Contraseña</label>
<input type="password" class="form-control" name="contraseña" placeholder="">
</div>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input">
<small>Recordarme</small>
</label>
<button type="submit" class="btn btn-login float-right" name="btn_entrar">Entrar</button>
</div>
</form>
</form>
And my code php process_login.php
<?php
include 'db.php';
if(isset($_POST['btn_entrar']))
{
$user=$_POST['usuario'];
$password=$_POST['contraseña'];
$admin="Administrador";
$vendedor="Vendedor";
$query_admin = "SELECT * FROM empleado WHERE usuario='$user'
AND contraseña='$password' AND cargo='$admin'";
$ejecutar_adnin = mysql_query($query_admin);
$query_ven = "SELECT * FROM empleado WHERE usuario='$user'
AND contraseña='$password' AND cargo='$vendedor'";
$ejecutar_vendedor = mysql_query($query_ven);
if(mysql_num_rows($ejecutar_adnin)==1)
{
header("location: index.php");
}
else if(mysql_num_rows($ejecutar_vendedor)==1)
{
header("location: index_empleados.php");
}
else
{
echo "<script>alert('Usuario o contraseña son incorrectos')</script>";
}
}
So, I have multiple forms on a page and I am trying to submit them conditionally using the same button, for some reason, I cannot trigger the submit button. Below is my code.
$("#save_contact").on("click", function () {
var contact_type = $("#contact_type").val();
console.log(contact_type);
if (contact_type == 2) {
console.log("here");
$('#supplier_form').submit();
} else if (contact_type == 3) {
} else if (contact_type == 4) {
} else if (contact_type == 5) {
} else if (contact_type == 1) {
}
});
This is my form, at the moment I have only created 1 form.
<div id="supplier_div" class="form_to_save">
<form id="supplier_form" name="supplier_form" action="{{route('save_reception_contacts')}}">
<hr>
<h4>Supplier</h4>
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Company</label>
<input type="text" name="company" id="company" />
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Notes</label>
<textarea name="notes" id="notes"></textarea>
</div>
</div>
</div>
<input type="hidden" name="contact_type_hidden">
<input type="hidden" name="user_type_hidden">
</form>
</div>
<div class="col-md-6">
<button type="button">Back</button>
<button type="button" class="save" id="save_contact">Save</button>
</div>
You have unclosed <hr> at
<form id="supplier_form" name="supplier_form" action="{{route('save_reception_contacts')}}">
<hr>
and an unclosed <div class="row">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="row">
otherwise I can't see anything wrong with submitting the form, you haven't provided relevant HTML that i could create the snippet.
You are creating a new variable and assigning the value of "contact_type" element. But I can not find this element in the HTML.
I have modified your code:
<div id="supplier_div" class="form_to_save">
<form id="supplier_form" name="supplier_form" action="{{route('save_reception_contacts')}}">
<hr></hr>
<h4>Supplier</h4>
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Company</label>
<input type="text" name="company" id="company" />
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>contact_type</label>
<input type="text" name="contact_type" id="contact_type" />
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Notes</label>
<textarea name="notes" id="notes"></textarea>
</div>
</div>
</div>
</div>
<input type="hidden" name="contact_type_hidden">
<input type="hidden" name="user_type_hidden">
</form>
</div>
<div class="col-md-6">
<button type="button">Back</button>
<button type="button" class="save" id="save_contact">Save</button>
</div>
Note that there is now a new input "contact_type".
And here is the script, where you will need to uncomment in order to send the form.
$("#save_contact").on("click", function() {
var contact_type = $("#contact_type").val();
if (contact_type == 2) {
console.log("2");
//$('#supplier_form').submit();
} else if (contact_type == 3) {
console.log("3");
//$('#supplier_form').submit();
} else if (contact_type == 4) {
console.log("4");
//$('#supplier_form').submit();
} else if (contact_type == 5) {
console.log("5");
//$('#supplier_form').submit();
} else if (contact_type == 1) {
console.log("1");
//$('#supplier_form').submit();
}
});
I am assuming you want to send the same and only form, not differents one, as it seems to understand from the initial question.
I have two form in one page. When I click on the submit button of form one, the validation error shows on both forms. How could show separate validation errors on each form?
This is my view:
<?php echo form_open('user_signup/login',['class'=>'login-form','id'=>'submit_form']);
echo validation_errors();?>
<h3 class="form-title font-green">Sign In</h3>
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span> Enter any username and password. </span>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Email</label>
<?php echo form_input(['name'=>'email1','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email1')]); ?>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<?php echo form_input(['name'=>'pass','type'=>'password','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Password']); ?>
</div>
<div class="form-actions">
<?php echo form_submit('submit', 'Submit',"class='btn green uppercase'" ); ?>
Forgot Password?
</div>
<div class="login-options">
<h4>Or login with</h4>
<ul class="social-icons">
<li>
<a class="social-icon-color facebook" data-original-title="facebook" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color twitter" data-original-title="Twitter" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color googleplus" data-original-title="Goole Plus" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color linkedin" data-original-title="Linkedin" href="javascript:;"></a>
</li>
</ul>
</div>
<div class="create-account">
<p>
Create an account
</p>
</div>
</form>
<!-- END LOGIN FORM -->
<!-- BEGIN FORGOT PASSWORD FORM -->
<form class="forget-form" action="http://keenthemes.com/preview/metronic/theme/admin_2/index.html" method="post">
<h3 class="font-green">Forget Password ?</h3>
<p> Enter your e-mail address below to reset your password. </p>
<div class="form-group">
<input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="Email" name="email" /> </div>
<div class="form-actions">
<button type="button" id="back-btn" class="btn green btn-outline">Back</button>
<button type="submit" class="btn btn-success uppercase pull-right">Submit</button>
</div>
</form>
<!-- END FORGOT PASSWORD FORM -->
<!-- BEGIN REGISTRATION FORM -->
<?php echo form_open('user_signup/login',['class'=>'register-form','id'=>'register_form']);
?>
<h3 class="font-green">Sign Up</h3>
<p class="hint"> Enter your personal details below: </p>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Roll NO</label>
<?php echo form_input(['name'=>'rollno','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Rollno','value'=>set_value('rollno')]); ?>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Email</label>
<?php echo form_input(['name'=>'email2','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email2')]); ?>
</div>
<div class="form-actions">
<button type="button" id="register-back-btn" class="btn green btn-outline">Back</button>
<?php echo form_submit('register', 'register',"class='btn btn-success uppercase pull-right'" ); ?>
This is my controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_signup extends CI_Controller {
public function index()
{
$this->load->view('User/signup');
}
public function login()
{
if ($this->input->post('submit_form')) {
$rules['email1'] = 'required';
$rules['pass'] = 'required';
$this->form_validation->set_rules($rules);
}
else if ($this->input->post('register_form')) {
$rules['rollno'] = 'required';
$rules['email2'] = 'required';
$this->validation->set_rules($rules);
}
if (!$this->form_validation->run()) {
$this->load->view('User/signup');
}
else {
if ($this->input->post('submit_form'))
echo 'Form 1 posted !';
else if ($this->input->post('register_form'))
echo 'Form 2 posted !';
}
}
}
There are two things you need to concern about that. First passing action in HTML form. Second is, getting those parameters and code accordingly in your controller's action.
<?php echo form_open('user_signup/login/form-1',['class'=>'login-form','id'=>'submit_form']);
echo validation_errors();?>
<h3 class="form-title font-green">Sign In</h3>
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span> Enter any username and password. </span>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Email</label>
<?php echo form_input(['name'=>'email1','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email1')]); ?>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<?php echo form_input(['name'=>'pass','type'=>'password','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Password']); ?>
</div>
<div class="form-actions">
<?php echo form_submit('submit', 'Submit',"class='btn green uppercase'" ); ?>
Forgot Password?
</div>
<div class="login-options">
<h4>Or login with</h4>
<ul class="social-icons">
<li>
<a class="social-icon-color facebook" data-original-title="facebook" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color twitter" data-original-title="Twitter" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color googleplus" data-original-title="Goole Plus" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color linkedin" data-original-title="Linkedin" href="javascript:;"></a>
</li>
</ul>
</div>
<div class="create-account">
<p>
Create an account
</p>
</div>
</form>
<!-- END LOGIN FORM -->
<!-- BEGIN FORGOT PASSWORD FORM -->
<form class="forget-form" action="http://keenthemes.com/preview/metronic/theme/admin_2/index.html" method="post">
<h3 class="font-green">Forget Password ?</h3>
<p> Enter your e-mail address below to reset your password. </p>
<div class="form-group">
<input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="Email" name="email" /> </div>
<div class="form-actions">
<button type="button" id="back-btn" class="btn green btn-outline">Back</button>
<button type="submit" class="btn btn-success uppercase pull-right">Submit</button>
</div>
</form>
<!-- END FORGOT PASSWORD FORM -->
<!-- BEGIN REGISTRATION FORM -->
<?php echo form_open('user_signup/login/form-2',['class'=>'register-form','id'=>'register_form']);
?>
<h3 class="font-green">Sign Up</h3>
<p class="hint"> Enter your personal details below: </p>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Roll NO</label>
<?php echo form_input(['name'=>'rollno','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Rollno','value'=>set_value('rollno')]); ?>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Email</label>
<?php echo form_input(['name'=>'email2','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email2')]); ?>
</div>
<div class="form-actions">
<button type="button" id="register-back-btn" class="btn green btn-outline">Back</button>
<?php echo form_submit('register', 'register',"class='btn btn-success uppercase pull-right'" ); ?>
Note: Check form actions
Here is your updated controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_signup extends CI_Controller {
public function index()
{
$this->load->view('User/signup');
}
public function login()
{
// Checking passed 3rd parameter in URI string.
$submitted_form = $this->uri->segment(3)?$this->uri->segment(3):false;
if ($submitted_form == 'form-1') {
$rules['email1'] = 'required';
$rules['pass'] = 'required';
$this->form_validation->set_rules($rules);
}
if ($submitted_form == 'form-2') {
$rules['rollno'] = 'required';
$rules['email2'] = 'required';
$this->validation->set_rules($rules);
}
if ($this->form_validation->run()) {
if ($submitted_form == 'form-1')
echo 'Form 1 posted !';
else if ($submitted_form == 'form-1')
echo 'Form 2 posted !';
}
$this->load->view('User/signup');
}
}
Let me know if you have any confusion.
Submit by Jquery. For Example in your view
<form id="form1" action="" method="post">
<label>Username</label>
<input type="text" name="username">
<label>Password</label>
<input type="password" name="password">
<input type="hidden" name="form_type" value="login">
<button type="submit" onClick="login()">Login</button>
</form>
<form id="form2" action="" method="post">
<label>Username</label>
<input type="text" name="username">
<label>Password</label>
<input type="password" name="password">
<label>Email</label>
<input type="email" name="email">
<input type="hidden" name="form_type" value="register">
<button type="submit" onClick="register()">Login</button>
</form>
Jquery Script
<script>
function login()
{
$('#form1').submit();
}
function register()
{
$('#form2').submit();
}
</script>
Now in your controller
if($_POST)
{
if($_POST['form_type']=='login')
{
// Validate Login
}
elseif($_POST['form_type']=='register')
{
// validate register
}
}
I created a contact form that is placed in the footer. my problem is when the form is submitted, it redirects me to the search results and it says
"No Results Found
The page you requested could not be found. Try refining your search, or use the navigation above to locate the post." I would like to know how can I make this work. THanks!
NOTE: I didn't use any plugin.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
// echo $_POST['email'];
echo "FORM SUBMITTED";
$msg=$_POST['msg'];
$email=$_POST['email'];
$name=$_POST['name'];
$subj=$_POST['action'];
$message=$name.' ('.$email.') :'.$msg;
wp_mail( 'psinc123#designsbyps.com', $subj , $message );
}
?>
<form method="POST" action="#" id="contactForm">
<div class="custom-contact-form">
<div class="first-part"><strong class="hey">Hey PS and Company,</strong><br>
<br>
I am <span id="name" contenteditable="true" class="contenteditable details">enter your name</span> and I would like<br>
<input type="hidden" id="name-field" name="name">
to <span id="action" class="contenteditable details chat-topic" style="cursor: pointer;">chat about a project</span>.
<input type="hidden" id="action-field" name="action">
<div id="submit" class="submit submit-first-part">
<div class="text">Next</div>
</div>
</div>
<div class="second-part">
<span id="question" contenteditable="true" data-content="Enter your message" class="contenteditable details">Enter your message</span><br>
<input type="hidden" id="msg-field" name="msg">
<br>My email address is<br><span id="email" contenteditable="true" data-content="you#email.com" class="contenteditable details">you#email.com</span>
<input type="hidden" id="email-field" name="email">
<div id="send" class="submit submit-second-part">
<div class="text">Send</div>
</div>
<div id="cancel" class="cancel">Cancel</div>
</div>
<div class="last-part">
<div id="message"></div><br><br>
<div id="return" class="cancel">Return</div>
</div>
<div class="overlay open">
<div class="choices-wrapper">
<div class="choices">
<div class="line active">know more about us</div>
<div class="line">chat about a project</div>
<div class="line">ask about a job</div>
<div class="line">say YO!</div>
</div>
</div>
</div>
</div>
</form>
You need this type structure :
function process-form () {
#
# -- form processing actions
#
return (0);
}
?> // end function process_form
.
.
.
name is a reserved term in WordPress. Use another key instead.
I am designing a website, and i encountered a problem.
In the following page "samgatha.org/register.php", wherever i click inside the form-box, it redirects to "samgatha.org/register.php". I am not able to find the problem.
Below i am posting the main code for the registeration form, i am not including the template code.
<div id="register-box">
<?php
require 'connection.php';
if($_SERVER['REQUEST_METHOD']=='POST') {
if((isset($_POST['enthu_name']))&&(isset($_POST['enthu_email']))&&(isset($_POST['enthu_contact']))&&(isset($_POST['college_name']))&&(isset($_POST['branch']))&&(isset($_POST['pass']))) {
$var_name = mysql_real_escape_string($_POST['enthu_name']);
if(!preg_match("/^[a-zA-Z ]*$/",$var_name)) {
die('Only letters and white spaces allowed in Name<br>');
}
$var_email = mysql_real_escape_string($_POST['enthu_email']);
if(!filter_var($var_email, FILTER_VALIDATE_EMAIL)) {
die("Invalid email format<br>");
}
$var_contact = mysql_real_escape_string($_POST['enthu_contact']);
$var_college = mysql_real_escape_string($_POST['college_name']);
$var_branch = mysql_real_escape_string($_POST['branch']);
$passwd = mysql_real_escape_string(md5($_POST['pass']));
$v1 = rand(0,getrandmax());
$v2 = rand(0,getrandmax());
$ac_conf = $v1.$v2;
$ac_conf_hash = md5($v1.$v2);
$v1 = rand(0,getrandmax());
$v2 = rand(0,getrandmax());
$fo_pass = $v1.$v2;
$query = "insert into student_detail (name,email,phno,college,branch,password,acc_confirm_code,forgot_pass_code)".
"values".
"('$var_name','$var_email','$var_contact','$var_college','$var_branch','$passwd','$ac_conf','$fo_pass')";
$retval = mysql_query($query);
if(!$retval) {
die('Could not register'.mysql_error());
}
$reg_conf_code = "http://samgatha.org/reg_conf.php?acconf=".$ac_conf_hash."&suse=".$var_email;
$reg_conf = "Please click on the link to activate<br>".$reg_conf_code;
mail($_POST['enthu_email'],"Samgatha Account Confirmation (no reply) link",$reg_conf);
header('Location: http://samgatha.org/login.php');
}
else {
echo "Please enter details to continue <br>";
}
}
?>
Welcome to samgatha registrations. <br>
Please fill out the following form to participate in samgatha.
<form id="sam_register" action="register.php" method="post">
<div class="reg-box-in">
<label class="i2" id="ii1" for="enthu_name">Name : </label>
<input class="i1" type="text" name="enthu_name" id="enthu_name"> <br>
</div>
<div class="reg-box-in">
<label class="i2" id="ii2" for="enthu_email">E-mail Address : </label>
<input class="i1" type="text" name="enthu_email" id="enthu_email"> <br>
</div>
<div class="reg-box-in">
<label class="i2" id="ii3" for="enthu_contact">Phone No : +91</label>
<input class="i1" type="text" name="enthu_contact" id="enthu_contact"> <br>
</div>
<div class="reg-box-in">
<label class="i2" id="ii4" for="college_name">Institute : </label>
<input class="i1" type="text" name="college_name" id="college_name"> <br>
</div>
<div class="reg-box-in">
<label class="i2" id="ii5" for="branch">Discipline : </label>
<input class="i1" type="text" name="branch" id="branch"> <br>
</div>
<div class="reg-box-in">
<label class="i2" id="ii6" for="password">Password : </label>
<input class="i1" type="text" name="pass" id="pass" maxlength="30"> <br>
</div>
<div class="reg-box-in">
<button type="submit" name="register">Register</button> <br>
</div>
</form>
</div>
And this is not the problem only with the register page.
You can check the website samgatha.org.
Various html issues
Unclosed link
<a href="login.php"><div id="register" class="lay2"><div id="registert">Sign In/Up</div></div>
<div id="register-box">
<?php
require 'connection.php';
if (!empty($_POST)) {
$var_name = mysql_real_escape_string(<?php echo htmlspecialchars($_POST["name"]); ?>);
//-- same used for post ------------
header('Location: http://samgatha.org/login.php');
}
else {
echo "Please enter details to continue <br>";
}
}
?>
Welcome to samgatha registrations. <br>
Please fill out the following form to participate in samgatha.
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
<div class="reg-box-in">
<label class="i2" id="ii1" for="enthu_name">Name : </label>
<input class="i1" type="text" name="enthu_name" id="enthu_name"> <br>
</div>
<div class="reg-box-in">
<label class="i2" id="ii2" for="enthu_email">E-mail Address : </label>
<input class="i1" type="text" name="enthu_email" id="enthu_email"> <br>
</div>
<div class="reg-box-in">
<label class="i2" id="ii3" for="enthu_contact">Phone No : +91</label>
<input class="i1" type="text" name="enthu_contact" id="enthu_contact"> <br>
</div>
<div class="reg-box-in">
<label class="i2" id="ii4" for="college_name">Institute : </label>
<input class="i1" type="text" name="college_name" id="college_name"> <br>
</div>
<div class="reg-box-in">
<label class="i2" id="ii5" for="branch">Discipline : </label>
<input class="i1" type="text" name="branch" id="branch"> <br>
</div>
<div class="reg-box-in">
<label class="i2" id="ii6" for="password">Password : </label>
<input class="i1" type="text" name="pass" id="pass" maxlength="30"> <br>
</div>
<div class="reg-box-in">
<button type="submit" name="register">Register</button> <br>
</form>
</div>
</div>
There is <a> tag on register-box which redirects to login.php. Please check your code it puts <a> tag.