redisplay PHP form with errors - php

I have a dozen fully functional scripts that use heredoc statements to display HTML including e.g. $errors[field_name] yet this nearly identical script results in POST errors like
"Notice: Undefined index: user_password in register.php on line 162"
When the user enters a password with 5 characters when 8+ characters are required, a validation function populates $errors['password'] and the form is supposed to re-display along with validation errors.
Can't see what I'm missing in this Bootstrap style form. The $errors array is global and I can force the display of errors, so I know errors are initialized but I keep getting
"Notice: Undefined index"
$fields = array( 'user_name', 'user_email', 'user_password', 'user_password2' );
function display_form($string = '')
{
global $error_token, $cfg, $fields, $form, $errors, $s, $token, $validation_status;
$error_message = '';
switch($_SERVER['REQUEST_METHOD'])
{
Case 'POST':
// validation failed, redisplay the form
$autofocus = '';
foreach($fields as $field){
$$field = htmlentities(get_cgi_var($field), ENT_QUOTES, 'UTF-8');
// wrap errors in Bootstrap alert markup
$errors[$field] = !empty($errors[$field]) ? '<div class="alert alert-danger">'.htmlentities($errors[$field], ENT_QUOTES, 'UTF-8').'</div>' : '';
// debug, force the premature display of errors.
// echo !empty($errors[$field]) ? '<div class="alert alert-danger">'.htmlentities($errors[$field], ENT_QUOTES, 'UTF-8').'</div>' : '<p>empty</p>';
}
break;
Case 'GET':
default:
$autofocus = 'autofocus';
foreach($fields as $field){
$$field = '';
$errors[$field] = '';
}
$error_token = '';
break;
};
foreach($fields as $field){
$validation_status[$field] = empty($errors[$field]) ? ' has-success' : ' has-error';
}
$self = SELF;
print<<<_HTML_
<div class="col-lg-6">
<div class="panel panel-primary">
<div class="panel-heading">Registration</div>
<div class="panel-body">
$string
$error_message
<form name="form1" id="form1" action="$self" class="form-horizontal" role="form" method="POST">
<fieldset>
$error_token
$errors[user_name]
<div class="row form-group$validation_status[user_name]">
<label for="user_name" class="col-lg-3 control-label">Name</label>
<div class="col-lg-9">
<input type="text" name="user_name" id="user_name" value="$user_name" maxlength="255" $autofocus required placeholder="Your First & Last Name" title="Your First & Last Name" autocomplete="on" class="form-control">
</div>
</div>
$errors[user_email]
<div class="row form-group$validation_status[user_email]">
<label for="user_email" class="col-lg-3 control-label">Email</label>
<div class="col-lg-9">
<input type="email" name="user_email" id="user_email" value="$user_email" maxlength="255" required placeholder="Your Email Address" autocomplete="on" class="form-control">
</div>
</div>
$errors[user_password]
<div class="row form-group$validation_status[user_password]">
<label for="user_password" class="col-lg-3 control-label">Password</label>
<div class="col-lg-9">
<input type="text" name="user_password" id="user_password" value="$user_password" maxlength="255" required placeholder="Password or Passphrase" autocomplete="on" class="form-control">
<p class="help-block">Case sensitive password containing 8+ characters or a phrase containing 3-5 words</p>
</div>
</div>
$errors[user_password2]
<div class="row form-group$validation_status[user_password2]">
<label for="user_password2" class="col-lg-3 control-label">Password</label>
<div class="col-lg-9">
<input type="text" name="user_password2" id="user_password2" value="$user_password2" maxlength="255" required placeholder="Re-enter Password or Passphrase" autocomplete="on" class="form-control">
</div>
</div>
<div class="row form-group">
<label for="submit_button" class="col-lg-3 control-label"> </label>
<div class="col-lg-9 text-center">
<input type="hidden" name="token" value="$token">
<input type="submit" name="submit_button" id="submit_button" value="Submit" onClick="return captcha_validation(this.form);" class="btn btn-primary">
</div>
</div>
</fieldset>
</form>
</div><!-- /panel-body -->
<div class="panel-footer text-center"> </div>
</div><!-- /panel -->
</div><!-- /col -->
<div class="col-lg-6">
<div class="well">unused column</div>
</div><!-- /col -->
_HTML_;
};

Here's your problem:
default:
$autofocus = 'autofocus';
foreach($fields as $field){
$$field = ''; // <------------------
$errors[$field] = '';
}
$error_token = '';
break;
replace $$field with $field or remove it altogether - it's not needed here.

Related

i've a problem with my form that i tried validating it with php then it should send the data to the firebase

it always gives me the first condition which gives me "Name Cannot be empty." and it dosen't send the data...
i tried changing the $_POST['inputs'] with variables but everytime it gives me undefined index
inside the script there's the code that sends the data to firebase
what seems to be the problem here
<form class="pb-5 ml-5" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<input type="text" class="form-control text-right" id="inputName" name="inputName">
<label for="staticEmail" class="col-sm-3 col-form-label text-left">الإسم</label>
<input type="email" class="form-control text-right" id="inputEmail" name="inputEmail">
<label for="staticEmail" class="col-sm-3 col-form-label text-left" >الإيميل</label>
<input type="password" class="form-control text-right" id="inputPassword" name="inputPassword">
<label for="staticPassword" class="col-sm-3 col-form-label text-left">كلمة المرور</label>
<input type="tel" id="inputNum" class="form-control text-right" name="inputNum">
<label for="staticNum" class="col-sm-3 col-form-label text-left" >رقم الموبايل</label>
<input type="hidden" name="form_submitted" value="1" />
<input type="button" value="اشترك" class="btn btn-danger w-50 mr-5" id="create-newuser-button" name="createUser">
</form>
</section>
</div>
<?php
$nameEmptyErr = $emailEmptyErr = $mobNumEmptyErr = $passwordEmptyErr = "";
$nameErr = $emailErr = $mobNumErr = $passwordErr = "";
$validation = true;
//Name Validation
if (empty($_POST['inputName'])) {
$nameEmptyErr = '<div class="error">
Name cannot be empty.
</div>';
echo $nameEmptyErr;
} else {
$name = test_input($_POST['inputName']);
//Email Validation
if (empty($_POST['inputEmail'])) {
$emailEmptyErr = '<div class="error">
Email cannot be empty.
</div>';
echo $emailEmptyErr;
} else {
$email = test_input($_POST['inputEmail']);
//Password Validation
if (empty($_POST['inputPassword'])) {
$passwordEmptyErr = '<div class="error">
Password cannot be empty.
</div>';
echo $passwordEmptyErr;
} else {
$password = test_input($_POST['inputPassword']);
//Mobile Number Validation
if (empty($_POST['inputNum'])) {
$mobNumEmptyErr = '<div class="error">
Mobile Number cannot be empty.
</div>';
echo $mobNumEmptyErr;
} else {
$mobNum = test_input($_POST['inputNum']);
$validation = true;
}
}
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_POST['createUser']) && $validation = true) :
?>
<script type='text/javascript'></script>
<?php
endif;
?>
Your page isn't testing to see if it has been submitted. As a result it displays your form and immediately starts validating the parameters - which aren't yet there.
You also don't seem to have a submit button, so there's no way to submit the form. If you have such a button you can test for it and do the validation if you find it.
Rework your page like this:
// Add a test for the presence of the submit button.
//If not found, display a form, otherwise do the validation
if (!isset($_POST['createUser'])) {
?>
<div>
<section>
<!-- No need to specify an action if the form is submitting to the same page. -->
<form class="pb-5 ml-5" method="POST">
<input type="text" class="form-control text-right" id="inputName" name="inputName">
<label for="staticEmail" class="col-sm-3 col-form-label text-left">الإسم</label>
<input type="email" class="form-control text-right" id="inputEmail" name="inputEmail">
<label for="staticEmail" class="col-sm-3 col-form-label text-left" >الإيميل</label>
<input type="password" class="form-control text-right" id="inputPassword" name="inputPassword">
<label for="staticPassword" class="col-sm-3 col-form-label text-left">كلمة المرور</label>
<input type="tel" id="inputNum" class="form-control text-right" name="inputNum">
<label for="staticNum" class="col-sm-3 col-form-label text-left" >رقم الموبايل</label>
<input type="hidden" name="form_submitted" value="1" />
<!-- Change this element to type="submit" -->
<input type="submit" value="اشترك" class="btn btn-danger w-50 mr-5" id="create-newuser-button" name="createUser">
</form>
</section>
</div>
<?php
} else {
// Do your validation here
}

Modal doesn't validate with PHP

Modal will not trigger my PHP validation.
I can't get my modal to use my PHP validation. HTML validation works. It connects to the DB and makes the updates, but skips the server-side validation. I would appreciate any help, this is new to me why a modal won't validate.
My PHP and Modal form is in one file. I know I need more error messages added to my form, but leaving the phone number and first name out triggers no error messages, so I will add more once I fix this issue. The code just executes and changes the DB.
<?php
//ini_set( 'display_errors', 1 );
//error_reporting( E_ALL );
session_start();
// Include db config
require_once 'includes/dbh.inc.php';
// Process form when post submit
// if($_SERVER['REQUEST_METHOD'] === 'POST'){
// Sanitize POST
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$error=array();
// $error = false;
// $error = isset($_SESSION['error']);
// $error = $_SESSION['error'];
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$first = test_input($_POST['first']);
$last = test_input($_POST['last']);
$email = test_input($_POST['email']);
$unit = $_SESSION['unit'];
$bed = test_input($_POST['bed']);
$bath = test_input($_POST['bath']);
$web = test_input($_POST['web']);
$phone = test_input($_POST['phone']);
$manage = test_input($_POST['manage']);
$ck = $_POST['rent'];
$id = $_SESSION['id'];
// Validate name
if(empty($first) || empty($last)){
$error['name'] = "Enter name";
}
// Validate email
if(empty($email)){
$error['email'] = "Please enter email";
}
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error['email'] = "Invalid email format";
}
// Validate name
if(empty($bed) || empty($bath)){
$error['rooms'] = 'Please enter bed/bath #';
}
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$web)) {
$error['web'] = "Invalid URL";
}
// Validate name
if(empty($phone)){
$error['phone'] = 'Please enter your phone';
}
//Check phone # format 000-000-0000
if(!preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone)) {
$error['phone'] = 'Please enter correct phone format';
}
//Check manage format
if (!preg_match("/^(\s\(([^)]+)\))?[[:punct:]]?\p{Lu}+(?:[\s'-]?[\p{L}\d]+)+(\(([^)]+)\))*$/", $manage)) {
$error['manage'] = 'Please enter correct management format';
}
if (empty($error)) {
var_dump($error);
print_r($error);
$sql = 'UPDATE condos_hp SET user_firstname=:first, user_lastname=:last, user_email=:email, user_bed=:bed, user_bath=:bath, user_web=:web, user_phone=:phone, user_manage=:manage, rent=:rent WHERE id=:id';
// Prepare statement
$stmt = $pdo->prepare($sql);
// Bind params
$stmt->bindParam(':first', $first, PDO::PARAM_STR);
$stmt->bindParam(':last', $last, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
// $stmt->bindParam(':unit', $unit);
$stmt->bindParam(':bed', $bed, PDO::PARAM_INT);
$stmt->bindParam(':bath', $bath, PDO::PARAM_INT);
$stmt->bindParam(':web', $web, PDO::PARAM_STR);
$stmt->bindParam(':phone', $phone, PDO::PARAM_STR);
$stmt->bindParam(':manage', $manage, PDO::PARAM_STR);
$stmt->bindParam(':rent', $ck, PDO::PARAM_INT);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute(array(':first'=>$first, ':last'=>$last, ':email'=>$email, ':bed'=>$bed, ':bath'=>$bath, ':web'=>$web, ':phone'=>$phone, ':manage'=>$manage, ':rent'=>$ck, ':id'=>$id));
// if($stmt){
// header('Location: index1.php');
//}
if (isset($_POST['update'])) {
// session_start();
//Then delete all SESSION variables
session_unset();
//And destroy the current session that is running
session_destroy();
// exit(header("Location: http://www.harbour-place.com/login0.php?update=success"));
if (headers_sent()) {
die("Redirect failed. Please click on this link: <a href=http://www.harbour-place.com/login0.php>");
}
else{
exit(header("Location: http://www.harbour-place.com/login0.php?update=success"));
}
}
}
include_once 'includes/mailer2.php';
?>
<!-- Button trigger modal -->
<div class="row justify-content-center">
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#exampleModalCenter">
UPDATE
</button>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="userupdateform" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">UPDATE RENTAL UNIT # <?php echo htmlentities($_SESSION['unit']) ?> </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div class="form-row justify-content-center">
<div class="form-group col-md-4">
<label >First Name</label>
<input type="text" class="form-control form-control-sm <?php echo (!empty($error['name'])) ? 'is-invalid' : ''; ?>" name="first" value="<?php echo htmlentities($_SESSION['first']) ?>">
<span class="invalid-feedback"><?php echo $error['name']; ?></span>
</div>
<div class="form-group col-md-4">
<label >Last Name</label>
<input type="text" class="form-control form-control-sm" name="last" value=<?php echo htmlentities($_SESSION['last']) ?> required>
</div>
</div>
<div class="form-row justify-content-center">
<div class="form-group col-md-8">
<label >Email</label>
<input type="text" class="form-control form-control-sm" name="email" value=<?php echo htmlentities($_SESSION['email']) ?> required>
</div>
<!--<div class="form-group col-md-4">
<label >Unit</label>
<input type="text" class="form-control form-control-sm" name="unit" value=<php echo $_SESSION['u_unit'] ?> required>
</div>-->
</div>
<div class="form-row justify-content-center">
<div class="form-group col-md-2">
<label >Bed</label>
<input type="text" class="form-control form-control-sm" name="bed" value=<?php echo htmlentities($_SESSION['bed']) ?> required>
</div>
<div class="form-group col-md-2">
<label >Bath</label>
<input type="text" class="form-control form-control-sm" name="bath" value=<?php echo htmlentities($_SESSION['bath']) ?> required>
</div>
<div class="form-group col-md-4">
<label >Web Site - www.myunit.com</label>
<input type="text" class="form-control form-control-sm" name="web" value=<?php echo htmlentities($_SESSION['web']) ?> required>
</div>
</div>
<div class="form-row justify-content-center">
<div class="form-group col-md-3">
<label >Phone 000-000-0000</label>
<input type="text" id="yourphone2" class="form-control form-control-sm <?php echo (!empty($error['phone'])) ? 'is-invalid' : ''; ?>" name="phone" value="<?php echo htmlentities($_SESSION['phone']) ?>">
<span class="invalid-feedback"><?php echo $error['phone']; ?></span>
</div>
<div class="form-group col-md-5">
<label >Managment Co (VRBO, Self,etc)</label>
<input type="text-capitalize" class="form-control form-control-sm" name="manage" value="<?php echo htmlentities( $_SESSION['manage']) ?>" required>
</div> </div>
<!--<div class="form-row justify-content-center">
<div class="form-group col-md-8">
<label >Managment Co - VRBO etc.</label>
<input type="text" class="form-control form-control-sm" name="manage" value="" required>
</div></div>-->
<div class="form-row justify-content-center">
<div class="custom-control custom-checkbox">
<input type="hidden" name="rent" value="0">
<input type="checkbox" value="1" name="rent" class="custom-control-input" id="customCheck1" checked="checked">
<label class="custom-control-label" for="customCheck1">Check to show on the rental site</label>
</div></div>
<p class="text-center mb-1"><small><i class="fas fa-lock"></i>Your Information is Safe With us!<br> You will need to re-login after clicking Update<br>This also updates the HP rental website listing. </small></p>
<button type="submit" class="btn btn-primary btn-sm" name="update">UPDATE HP UNIT</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
There are a couple issues here
You are setting $error to a boolean value, trying to define an array key with a value on a boolean doesn't work, it will continue to just be a boolean [Edit] if the value of $error evaluates to false before assigning array key => values to it, it will convert to an array but if it evaluates to true it will result in Warning: Cannot use a scalar value as an array and continue to be a boolean
You are never checking $error to stop the script from executing and updating the database

Fatal error of PHP [duplicate]

This question already has answers here:
Isset expression error
(3 answers)
Closed 6 years ago.
I'm doing an admin panel and I'm doing a form for adding users. My code below:
<form class="form-horizontal">
<fieldset>
<!-- Form Name -->
<legend>Add user</legend>
<!-- Prepended text-->
<div class="form-group">
<label class="col-md-4 control-label" for="newuser">Username</label>
<div class="col-md-4">
<div class="input-group">
<span class="input-group-addon">#</span>
<input id="newuser" name="newuser" class="form-control" placeholder="Username" type="text" required="">
</div>
<p class="help-block">Put the new users username here</p>
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="userpw">New user password</label>
<div class="col-md-4">
<input id="userpw" name="userpw" type="password" placeholder="Password" class="form-control input-md" required="">
<span class="help-block">Enter new user's password here!</span>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="permissions">Choose permissions</label>
<div class="col-md-4">
<select id="permissions" name="permissions" class="form-control">
<option value="1">System administrator</option>
<option value="2">Editor</option>
<option value="3">Developer</option>
<option value="4">Normal user</option>
</select>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="btn_send">Send request to server</label>
<div class="col-md-4">
<button id="btn_send" name="btn_send" class="btn btn-primary">Send it!</button>
</div>
</div>
</fieldset>
</form>
<?php
$newuser = $_GET['newuser'];
$userpw = $_GET['userpw'];
$permission = $_GET['permissions'];
if(isset($newuser and $userpw and $permission) {
$sql = "INSERT INTO users(username,userpw,permission)VALUES('$newuser', '$userpw','$permission')";
mysql_query($sql);
}
else {
echo "error";}
And I Get:
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in C:\xampp\htdocs\lumino\usermanagement.php on line 95
I couldn't fix it anyhow, playing with this for like 5 hours.
It's doing $_GET method, so I get the url like:
mysite.com/usermanagement.php?newuser=Berkay&userpw=18042003&permissions=3&btn_send=
Using isset, you can't have multiple items inside it:
if(isset($newuser and $userpw and $permission) {
Needs to become:
if(isset($newuser) && isset($userpw) && isset($permission)) {
Or:
if(isset($newuser, $userpw, $permission)) {
You just miss a ) and a set of ,:
if(isset($newuser, $userpw, $permission)) {
$sql = "INSERT INTO users(username,userpw,permission)VALUES('$newuser', '$userpw','$permission')";
mysql_query($sql);
}

PHP mailer form data verification isn't working

NOTE: the mail function itself works fine, I have a hosted site with mail configured professionally. Mail is sent via this script, but my simple empty field handler never catches empty fields. I'm sure this is something super obvious, but I'm very new to php and the logic of my script seems sound. The code follows:
edit: I've left the form call within the php script in case someone finds issue with the bootstrap form itself. The redirect is temporary until I can get better form error handling going.
<body>
<!-- jQuery and bootstrap -->
<script src="https://code.jquery.com/jquery.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/counter.js"></script>
<div class="jumbotron-fluid" id="primary">
<?php
if (isset($_POST['submit'])) {
$from = 'Web_Form_Contact';
$to = 'example#example.com';
$subject = 'Job_Contact';
$c_type = $_POST["c_type"];
if (empty($_POST["c_name"])) {
$errName = 'Info missing: Name';
} else {
$c_name = $_POST["c_name"];
}
if (empty($_POST["c_email"] )) {
$errEmail = 'Please enter a valid email address';
} else {
$c_email = $_POST["c_email"];
}
if (empty($_POST["co_name"])) {
$errCoName = 'Info missing: Company Name';
} else {
$co_name = $_POST["co_name"];
}
if (empty($_POST["j_desc"])) {
$errjDesc = 'Info missing: Job Description';
} else {
$j_desc = $_POST["j_desc"];
}
$body = "From: $c_name\n E-Mail: $c_email\n Type: $c_type\n CoName: $co_name\n Description\n $j_desc";
if (empty($errName && $errEmail && $errCoName && $errjDesc)) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch\n This page will redirect in 5 seconds.</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
} else {
echo "$errName\n $errEmail\n $errCoName\n $errjDesc";
}
}
echo "$result";
?>
<div class="container">
<form class="form-horizontal" role="form" method="post" action="php/info.php"
<div class="row">
<div class="col-xs-10 col-sm-6 col-md-6 col-lg-offset-2 col-md-offset-2">
<<h2>Contact Me! <small></small></h2>
<form>
<div class="form-group row">
<label for="clientEmail" class="col-sm-2 form-control-label">Email</label>
<div class="col-sm-10">
<input type="email" name="c_email"class="form-control" id="clientEmail" placeholder="Email" >
</div>
</div>
<div class="form-group row">
<label for="clientName" class="col-sm-2 form-control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="c_name" id="clientName" placeholder="Your Name" >
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">Contact Type</label>
<div class="col-sm-10">
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios1" value="recruiter" checked>
I am a recruiter, and do not work directly for the company hiring.
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios2" value="employee">
I work for the company in question, and wish to discuss an open position.
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios3" value="other" >
Other
</label>
</div>
</div>
</div>
<div class="form-group row">
<label for="coName" class="col-sm-2 form-control-label">Company Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="co_name" id="coName" placeholder="Company Name" >
</div>
</div>
<!-- Job Description Text Box -->
<div class="form-group row">
<label for="Job Description ">Job Description: (1100 character max!)</label>
<div class="col-sm-10 col-lg-offset-2 col-md-offset-2 col-xl-offset-2 col-sm-offset-2" >
<div class="form-group">
<textarea class="form-control" rows="5" name="j_desc" id="jobDesc" placeholder="Job Description" ></textarea>
<p class="counter">1100</p>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="send" name="submit" value="Send"class="btn btn-secondary">Submit</button>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<!--redirect script -->
<script>
function redirect(page) {
setTimeout(function () {
window.location.href = page;
}, 5000);
}
//sends to homepage
redirect("../index.html");
</script>
You need to have an empty check for each variable:
if (empty($errName) && empty($errEmail) && empty($errCoName) && empty($errjDesc)) {
You can also set an email flag at the beginning to true, and if you hit any of the errors, you can set it to false. Then when you're ready to email, check to see if that email flag is still true.

Unable to retrieve ID to update in database

I am unable to identify the mistake
model (here I am matching the id to update)
function get_account_record($a_id)
{
$this->db->where('a_id', $a_id);
$this->db->from('account_info');
$query = $this->db->get();
return $query->result();
}
controller (receiving everything via post accept the ID i-e a_id )
function update($a_id)
{
$data['a_id'] = $a_id;
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
form Validation
if ($this->form_validation->run() == FALSE)
{
$this->load->view('viewUpdate', $data);
}
else {
$data = array(
'a_name' => $this->input->post('a_name'),
'a_website' => $this->input->post('a_web'),
'a_billingStreet' => $this->input->post('a_billingStreet'),
'a_mobile' => $this->input->post('a_mobile'),);
$this->db->where('a_id', $a_id);
$this->db->update('account_info', $data);
display success message
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Successfully Updated!</div>');
//redirect('salesforce' . $a_id);
}
}
view (ID and Name)
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_id" class="control-label">Account ID</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_id" name="a_id" placeholder="" disabled="disabled" type="text" class="form-control" value="<?php echo $a_id;?>" />
<span class="text-danger"><?php echo form_error('a_id'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_name" class="control-label">Account Name</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_name" name="a_name" placeholder="Enter Account Name" type="text" class="form-control" value="<?php echo $view[0]->a_name; ?>" />
<span class="text-danger"><?php echo form_error('a_name'); ?></span>
</div>
</div>
</div>
If you don't want to allow the user to edit then add readonly instead of disabled attribute
<div class="col-lg-8 col-sm-8">
<input id="a_id" name="a_id" placeholder="" readonly="readonly" type="text" class="form-control" value="<?php echo $a_id;?>" />
<span class="text-danger"><?php echo form_error('a_id'); ?></span>
</div>
If you want to disable then add the hidden element
<input id="a1_id" name="a1_id" type="hidden" value="<?php echo $a_id;?>" />
Controller Function
function update($a_id)
{
//read the value using input library
$a_id = $this->input->post('a_id');
//$a1_id = $this->input->post('a1_id'); your hidden element value can be get here.
$data['a_id'] = $a_id;
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
}
My problem Solved after editing the where clause in update query
$this->db->where('a_id', $_POST['a_id']);

Categories