registration script keeps showing errors when all fields are filled - forms

Im having a little issue with my registration script.
I am trying to add rows into my mysql database.
This is my form verifier to make sure all fields are filled:
//Variables from the table
$user = $_POST['un'];
$pass = $_POST['pw'];
$rpass = $_POST['rpw'];
//Prevent MySQL Injections
$user = stripslashes($user);
$pass = stripslashes($pass);
$rpass = stripslashes($rpass);
$user = mysqli_real_escape_string($user);
$pass = mysqli_real_escape_string($pass);
$rpass = mysqli_real_escape_string($rpass);
//Check to see if the user left any space empty!
if($user == "" || $pass == "" || $rpass == "")
{
echo "Please fill in all the information!";
}
and this is the form
<form action="register_proc.php" method="post">
<label for="un">Username</label>
<input type="text" name="un" id="un" placeholder="username"/>
<br/>
<label for="pw">Password</label>
<input type="password" name="pw" id="pw" placeholder="password"/>
<br/>
<label for="rpw">Repeat Password</label>
<input type="password" name="rpw" id="rpw" placeholder="repeat password"/>
<br/>
<input type="submit" name="submit" value="Complete Registration"><br>
<div style="float: left;">
Home
</div>
<div style="float: right;">
Login
</div>
</form>
I have no idea why it keeps outputting the else statment "Please fill in all the information!"
All of the $post variables have the correct names
thank you for the upcoming support.

Related

Handler method not firing when called by form in Razor Pages

I've gone through dozens of articles, docs, and stack overflow questions (even the one with a similar intro)regarding the same issues but it still persists.
I've tried this with putting the functions in the .cshtml.cs page and on the .cshtml page, named and unnamed handler names, different framework for sending emails, and adding an empty action field in the form along with other fixes but the issue seems to be that the handler method itself is not firing while the form is submitting. Any and all help is appreciated and please let me know if more information is needed.
My HTML form:
<form method="POST" asp-page-handler="email">
<!-- Name input-->
<div class="form-floating mb-3">
<input class="form-control" name="clientName" type="text" placeholder="Enter your name..." required/>
<label for="name">Full name*</label>
</div>
<!-- Email address input-->
<div class="form-floating mb-3">
<input class="form-control" name="clientEmail" type="email" placeholder="name#example.com" required/>
<label for="email">Email address*</label>
</div>
<!-- Phone number input-->
<div class="form-floating mb-3">
<input class="form-control" name="clientPhone" type="tel" placeholder="(123) 456-7890"/>
<label for="phone">Phone number</label>
</div>
<!-- Message input-->
<div class="form-floating mb-3">
<textarea class="form-control" name="clientMessage" type="text" placeholder="Enter your message here..." style="height: 10rem" required></textarea>
<label for="message">Message*</label>
</div>
<!-- Submit Button-->
<div class="d-grid"><button class="btn btn-primary btn-xl" type="submit" value="submit">Submit</button></div>
</form>
My functions as they are currently:
public void OnPostEmail()
{
var clientEmail = Request.Form["clientEmail"];
var clientName = Request.Form["clientName"];
var clientPhone = Request.Form["clientPhone"];
var clientMessage = Request.Form["clientMessage"];
sendEmail(clientEmail, clientName, clientPhone, clientMessage);
}
public void sendEmail(string clientEmail, string clientName, string clientPhone, string clientMessage)
{
var errorMessage = "";
try
{
// Initialize WebMail helper
WebMail.SmtpServer = "smtp.google.com";
WebMail.SmtpPort = 587;
WebMail.UserName = "***#gmail.com";
WebMail.Password = "MYPASSWORD";
WebMail.From = "***#gmail.com";
WebMail.EnableSsl = true;
// Send email
WebMail.Send(to: clientEmail,
subject: $"Request from: + {clientName}",
body: $"{clientMessage}\nPhone: {clientPhone}\nEmail: {clientEmail}"
);
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}

Codeigniter appears to always set validation->run to FALSE

Controller : User.php
public function index()
{
log_message('debug', ' Index User');
die("should never get here");
}
public function login()
{
log_message('debug', ' Login Entered');
echo '<pre> Printing Login data:';
print_r($_POST);
echo "</pre>";
$data['pageHeader'] = "Login";
$data['message'] = 'temporary message - This will be the login stuff';
$this->session->set_flashdata('flashInfo', 'Login form will go here');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() === FALSE)
{
log_message('debug', ' validation run FALSE');
$this->load->helper('form');
$this->render_page('user/login', 'public_header', 'footer', $data);
}
else
{
log_message('debug', ' validation run TRUE');
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('username'), $this->input->post('password'), $remember))
{
log_message('debug', ' redirect Dashboard');
redirect('dashboard');
}
else
{
$_SESSION['auth_message'] = $this->ion_auth->errors();
$this->session->mark_as_flash('auth_message');
log_message('debug', ' redirect user login');
redirect('user/login');
}
}
}
Known from log:
function login entered,
$_POST always 'empty',
form validation-> always FALSE thus debug message is logged and form redisplayed
Form user/login.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<div class="container">
<?php echo $message;?>
<h1>Login</h1>
<form action="#" method="post">
<div class="imgcontainer">
<img src="http://www.hdkumdo.com/smen/assets/crow2.png" alt="Swordsmen Martial Arts">
</div>
<div class="container">
<label for="username"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="username" id="username" required autofocus>
<label for="password"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" id="password" required>
<button type="submit" name="login" id="login" value"login" >Login</button>
<label>
<input type="checkbox" checked="checked" name="remember" id="remember"> Remember me
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<span class="psw">Forgot password?</span>
</div>
</form>
</div>
Copied an example from https://www.codeigniter.com/user_guide/libraries/form_validation.html
and form posts data but if I change function index to login it does not work.
Menu link : Login works fine.
Everything seems to be connected...login form displays, Controller function login is entered but it seems like there is no POST data so the form is simply redisplayed. I have 2 simple validation rules so it seems that validation->run should return true if I just enter any data into the fields.
I am sure it's something simple but for the life of me I can't see what.
First of all make sure you have loaded form validation library
$this->load->library('form_validation');
//set field validation
$this->form_validation->set_rules('fieldname','Field Name','required');
if($this->form_validation->run())
{
//then process form
//redirect
}
else {
//load view
}

how to get the name of image in input , code igniter edit form

I am trying to edit a picture that I've already uploaded, so when Click on edit i cant not get the name of the image in the input. here is my code in controller:
It means I want to edit the details of an item but i don't want to edit the image of it.
controller
//method to edit category
public function edit_menu($id) {
if($this->isLogin()) {
//object/instance of menu
$menu_list = new Menu();
//calling method of , menu model
$menu_list->load($id);
//print_r($menu_list);
$menu_list_array = (array)$menu_list;
//object/instance of category
$category = new Category();
//calling method of category model
$category_list = $this->Category->get();
//creating an array to hold data
$category_form_options = array();
//iterate through loop
foreach ($category_list as $id => $category) {
$category_form_options[$id] = $category->category_name;
}
//creating an array to hold data for category and menu, to pass data to view
$data = array();
//category key of data array to hold category details
$data['category'] = $category_form_options[$menu_list_array['category_id']];;
//menu key of data array to hold menu details
$data['menu'] = $menu_list;
//CI built-in method to have three params, first is field id, second is human readable lable to show in case of error, rules include required, type.
$this->form_validation->set_rules(array(
array('field' => 'category_id','label' => 'Category', 'rules' => 'required',),
array('field' => 'type','label' => 'Type','rules' => 'required',),
array('field' => 'menu_name', 'label' => 'Menu Name', 'rules' => 'required',),
array('field' => 'description','label' => 'Description','rules' => 'required',),
));
if (empty($_FILES['avatar']['name'])){
$this->form_validation->set_rules('avatar', 'Avatar', 'required');
}
//CI method to error delimiters,
//https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#errordelimiters
$this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div>');
if (!$this->form_validation->run()) {
$datas['title'] = "";
$this->load->view('header', $datas);
//this will load data
$this->load->view('category_form_edit', array('category_form_options' => $category_form_options, 'data' =>$data, ));
$this->load->view('footer');
}
else {
$config['upload_path'] = './assets/images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->upload->do_upload('avatar');
$data_upload_files = $this->upload->data();
//record from global post variable to model fields.
$menu_data = array();
$menu_data['category_id'] = $menu_list->category_id = $this->input->post('category_id');
$menu_data['type'] = $menu_list->type = $this->input->post('type');
$menu_data['menu_name'] = $menu_list->menu_name = $this->input->post('menu_name');
$menu_data['description'] = $menu_list->description = $this->input->post('description');
$menu_data['per_gram'] = $menu_list->per_gram = $this->input->post('per_gram');
$menu_data['per_quantity'] = $menu_list->per_quantity = $this->input->post('per_quantity');
$menu_data['per_dozen'] = $menu_list->per_dozen = $this->input->post('per_dozen');
$menu_data['avatar'] = $menu_list->avatar = $data_upload_files['file_name'];
$menu_data['updated_by'] = $menu_list->updated_by = $_SESSION['user_id'];
$menu_data['updated_on'] = $menu_list->updated_on = date('Y-m-j H:i:s');
//update model
$id = $menu_list_array['menu_id'];
$where = array('menu_id' => $id);
$menu_list->update('menus', $menu_data, $where);
$this->load_menu();
}
}
}
view
<?php echo form_open_multipart('', 'fscontrol/edit_menu');?>
<!-- <form method="post"> -->
<div class="form-group">
<label for="category_id">Category Name</label>
<select name="category_id" class="form-control" >
<?php
foreach ($category_form_options as $category_id => $category_name) {
$selected = $data_array['category_id'] == $category_id ? 'selected' : '';
echo '<option ' . $selected. ' value="' . html_escape($category_id) . '">' . html_escape($category_name) . '</option>';
}
?>
</select>
</div>
<div class="form-group">
<label for="type">Type</label>
<input type="text" class="form-control" name="type" value="<?php echo $data_array['type']; ?>">
</div>
<div class="form-group">
<label for="menu_name">Menu Name</label>
<input type="text" class="form-control" name="menu_name" value="<?php echo $data_array['menu_name']; ?>">
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" value="<?php echo $data_array['description']; ?>">
</div>
<div class="form-group">
<label for="per_gram">Per gram</label>
<input type="text" class="form-control" name="per_gram" value="<?php echo $data_array['per_gram']; ?>">
</div>
<div class="form-group">
<label for="per_quantity">Per quantity</label>
<input type="text" class="form-control" name="per_quantity" value="<?php echo $data_array['per_quantity']; ?>">
</div>
<div class="form-group">
<label for="per_dozen">Per dozen</label>
<input type="text" class="form-control" name="per_dozen" value="<?php echo $data_array['per_dozen']; ?>">
</div>
<div class="form-group">
<label for="avatar">Photo</label>
<input type="file" class="form-control" name="avatar" size="20" value="<?php echo $data_array['avatar']; ?>">
</div>
<div class="form-group">
<input type="hidden" name="menu_id" value="<?php echo $data_array['menu_id']; ?>">
<input type="submit" class="btn btn-default" value="Edit"/>
</div>
</form>
If you are using bootstrap template you will not get the image name. You'll see only:
no file chosen
So it is better to use:
<img src="">
, and display image as a thump nail.

Form input OR drop menu

Hi I'm trying to make a form where on the name field you can use EITHER text or drop menu (example the drop menu would contain names already in the database whereas the text input would be for new names)
<div class="form-group form-group-sm" style="margin: 1px;">
<label class="col-sm-2 control-label" for="Owner">Owner Name: </label>
<div class="col-sm-4"><input class="form-control" type="text" id="Owner" name="Owner" <?php $q = $opened['Owner']; if(isset($opened['IDNumber'])) { echo "value=$q"; } else { echo "placeholder='Enter Owner Name'";}; ?> required></div>
<div class="col-sm-1">Or Select</div>
<div class="col-sm-4"><select class="form-control" id="Owner" name="Owner" required>
<option>Select</option>
<?php
$sql3="SELECT DISTINCT Owner FROM list Order by Owner asc";
$results = mysqli_query($dbcon,$sql3) or die();
while($row = mysqli_fetch_array($results)) {
echo '<option value="'.$row['Owner'].'">'.htmlspecialchars($row['Owner']).'</option>';
}
?>
</select></div>
</div>
how would i get it to input the data from one OR the other? not both

Login change in to signup

I have created a HTML5 registration form,is it possible for it to check the email address(inserted first) if it's already used to ask only for password(login) and if it's not to insert other fields needed for signup? using php,js or others.
<form id="signin_student" class="form-signin" method="post">
<h3 class="form-signin-heading"><i class="icon-lock"></i> Register </h3>
<input type="text" class="input-block-level" id="username" name="username" placeholder="NISN" required>
<input type="text" class="input-block-level" id="firstname" name="firstname" placeholder="Nama Depan" required>
<input type="text" class="input-block-level" id="lastname" name="lastname" placeholder="Nama Belakang" required>
<label>Kelas</label>
<select name="class_id" class="input-block-level span5">
<option>Pilih Kelas</option>
<?php
$query = mysql_query("select * from class order by class_name ")or die(mysql_error());
while($row = mysql_fetch_array($query)){
?>
<option value="<?php echo $row['class_id']; ?>"><?php echo $row['class_name']; ?></option>
<?php
}
?>
</select>
<input type="password" class="input-block-level" id="password" name="password" placeholder="Password" required>
<input type="password" class="input-block-level" id="cpassword" name="cpassword" placeholder="Tulis Ulang Password" required>
<button title="Klik untuk Daftar" id="signin" name="login" class="btn btn-info" type="submit"><i class="icon-check icon-large"></i> Daftar</button>
</form>
<script>
jQuery(document).ready(function(){
jQuery("#signin_student").submit(function(e){
e.preventDefault();
var password = jQuery('#password').val();
var cpassword = jQuery('#cpassword').val();
if (password == cpassword){
var formData = jQuery(this).serialize();
$.ajax({
type: "POST",
url: "student_signup.php",
data: formData,
success: function(html){
if(html=='true')
{
var delay = 2000;
setTimeout(function(){ window.location = 'siswa/index.php' }, delay);
}else if(html=='false'){
{ header: 'Data in DB is Not Found' });
}
}
});
}else
{
$.jGrowl("student does not found in the database", { header: 'Sign Up Failed' });
}
});
});
</script>
and file signup.php
<?php
mysql_select_db('db',mysql_connect('localhost','root',''))or die(mysql_error());
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$class_id = $_POST['class_id'];
$query = mysql_query("select * from student where username='$username' and firstname='$firstname' and lastname='$lastname' and class_id = '$class_id'")or die(mysql_error());
$row = mysql_fetch_array($query);
$id = $row['student_id'];
$count = mysql_num_rows($query);
if ($count > 0){
mysql_query("update student set password = '$password', status = 'Registered' where student_id = '$id'")or die(mysql_error());
$_SESSION['id']=$id;
echo 'true';
}else{
echo 'false';
}
?>
Firstly, you could use this code to check when the user stops typing:
Run javascript function when user finishes typing instead of on key up?
Once that is finished, this SQL code may work to check if the email already exists:
SELECT EXISTS(SELECT email FROM email_table)
Then you can simply use a conditional statement to print whether the email exists.