Form Data is not getting stored in database - javascript

I have Two new fields here:
Its Number & Mauze but I am unable to see data in database:
The form gets submitted successfully but i cannot find the values of these two fields in my database
here's my code:
For form fields:
<div class="col-md-6">
<!-- custom text field -->
<label for='its' class="mycss1"><?php esc_html_e('ITS Number', 'masterstudy-lms-learning-management-system'); ?></label>
<input type="text"
name="its"
placeholder="Enter your ITS Number"
class="mycss"
kr-icon="fas fa-envelope"
required/>
</div>
<div class="col-md-6">
<!-- custom text field -->
<label for='mauze' class="mycss1"><?php esc_html_e('Enter your Mauze', 'masterstudy-lms-learning-management-system'); ?></label>
<input type="text"
name="mauze"
placeholder="Mauze"
class="mycss"
kr-icon="fas fa-envelope"
required/>
</div>`
for database connection:
<?php
if (isset($_POST['register'])) {
extract($_POST);
$servername = "localhost ";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO `wp_usermeta` (its,mauze)
VALUES ('$its','$mazue')";
if ($conn->query($sql) === TRUE) {
header('Location: register.php');
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>

Here's my full form data its an signup form
<?php
stm_lms_register_style('register');
enqueue_register_script();
$r_enabled = STM_LMS_Helpers::g_recaptcha_enabled();
$disable_instructor = STM_LMS_Options::get_option('register_as_instructor', false);
if ($r_enabled):
$recaptcha = STM_LMS_Helpers::g_recaptcha_keys();
endif;
$site_key = (!empty($recaptcha['public'])) ? $recaptcha['public'] : '';
if (class_exists('STM_LMS_Form_Builder')):
$additional_forms = STM_LMS_Form_Builder::register_form_fields();
$default_fields = STM_LMS_Form_Builder::profile_default_fields_for_register();
$register_form = $additional_forms['register'];
$become_instructor = $additional_forms['become_instructor'];
?>
<script>
window.profileDefaultFieldsForRegister = <?php echo sanitize_text_field(json_encode($default_fields)); ?>;
window.additionalRegisterFields = <?php echo sanitize_text_field(json_encode($register_form)); ?>;
window.additionalInstructorsFields = <?php echo sanitize_text_field(json_encode($become_instructor)); ?>;
</script>
<?php
endif;
?>
<div id="stm-lms-register"
class="vue_is_disabled"
v-init="site_key = '<?php echo stm_lms_filtered_output($site_key); ?>'"
v-bind:class="{'is_vue_loaded' : vue_loaded}">
<h3><?php esc_html_e('Sign Up', 'masterstudy-lms-learning-management-system'); ?></h3>
<form #submit.prevent="register()" class="stm_lms_register_wrapper">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="heading_font"><?php esc_html_e('Username', 'masterstudy-lms-learning-management-system'); ?></label>
<input class="form-control"
type="text"
name="login"
v-model="login"
placeholder="<?php esc_html_e('Enter username', 'masterstudy-lms-learning-management-system'); ?>"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="heading_font"><?php esc_html_e('E-mail', 'masterstudy-lms-learning-management-system'); ?></label>
<input class="form-control"
type="email"
name="email"
v-model="email"
placeholder="<?php esc_html_e('Enter your E-mail', 'masterstudy-lms-learning-management-system'); ?>"/>
</div>
</div>
<div class="col-md-6">
<!-- custom text field -->
<label for='its' class="mycss1"><?php esc_html_e('ITS Number', 'masterstudy-lms-learning-management-system'); ?></label>
<input type="text"
name="its"
placeholder="Enter your ITS Number"
class="mycss"
kr-icon="fas fa-envelope"
required/>
</div>
<div class="col-md-6">
<!-- custom text field -->
<label for='mauze' class="mycss1"><?php esc_html_e('Enter your Mauze', 'masterstudy-lms-learning-management-system'); ?></label>
<input type="text"
name="mauze"
placeholder="Mauze"
class="mycss"
kr-icon="fas fa-envelope"
required/>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="heading_font"><?php esc_html_e('Password', 'masterstudy-lms-learning-management-system'); ?></label>
<input class="form-control"
type="password"
name="password"
v-model="password"
placeholder="<?php esc_html_e('Enter password', 'masterstudy-lms-learning-management-system'); ?>"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="heading_font"><?php esc_html_e('Password again', 'masterstudy-lms-learning-management-system'); ?></label>
<input class="form-control"
type="password"
name="password_re"
v-model="password_re"
placeholder="<?php esc_html_e('Confirm password', 'masterstudy-lms-learning-management-system'); ?>"/>
</div>
</div>
<div v-for="(profileField, index) in profileDefaultFieldsForRegister" class="col-md-12">
<div class="form-group">
<label class="heading_font" v-html="profileField.label"></label>
<input class="form-control" v-if="index !== 'description'" type="text" v-model="profileField.value" :placeholder="profileField.placeholder" :required="profileField.required" />
<textarea class="form-control" v-if="index === 'description'" v-model="profileField.value" :placeholder="profileField.placeholder" :required="profileField.required"></textarea>
</div>
</div>
</div>
<div class="row additional-fields" v-if="additionalRegisterFields.length"
v-for="(field, index) in additionalRegisterFields">
<div class="col-md-12">
<div class="form-group">
<label class="heading_font" v-if="typeof field.label !== 'undefined'" v-html="field.label"></label>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/email'); ?>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/select'); ?>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/radio'); ?>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/textarea'); ?>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/checkbox'); ?>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/file', array('name' => 'register')); ?>
<div class="field-description" v-if="field.description" v-html="field.description"></div>
</div>
</div>
</div>
<transition name="slide-fade">
<div class="row" v-if="become_instructor && !additionalInstructorsFields.length">
<div class="col-md-6">
<div class="form-group">
<label class="heading_font"><?php esc_html_e('Degree', 'masterstudy-lms-learning-management-system'); ?></label>
<input class="form-control"
type="text"
name="degree"
v-model="degree"
placeholder="<?php esc_html_e('Enter Your Degree', 'masterstudy-lms-learning-management-system'); ?>"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="heading_font"><?php esc_html_e('Expertise', 'masterstudy-lms-learning-management-system'); ?></label>
<input class="form-control"
type="text"
name="expertize"
v-model="expertize"
placeholder="<?php esc_html_e('Enter your Expertize', 'masterstudy-lms-learning-management-system'); ?>"/>
</div>
</div>
</div>
</transition>
<div class="row additional-fields" v-if="become_instructor && additionalInstructorsFields.length"
v-for="(field, index) in additionalInstructorsFields">
<div class="col-md-12">
<div class="form-group">
<label class="heading_font" v-if="typeof field.label !== 'undefined'" v-html="field.label"></label>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/email'); ?>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/select'); ?>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/radio'); ?>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/textarea'); ?>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/checkbox'); ?>
<?php STM_LMS_Templates::show_lms_template('account/v1/form_builder/file', array('name' => 'becomeInstructor')); ?>
<div class="field-description" v-if="field.description" v-html="field.description"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<?php STM_LMS_Templates::show_lms_template('gdpr/privacy_policy'); ?>
<?php do_action('stm_lms_register_custom_fields'); ?>
<div class="stm_lms_register_wrapper__actions">
<?php ?>
<?php if (!$disable_instructor): ?>
<label class="stm_lms_styled_checkbox">
<span class="stm_lms_styled_checkbox__inner">
<input type="checkbox"
name="become_instructor"
v-model="become_instructor"/>
<span><i class="fa fa-check"></i> </span>
</span>
<span><?php esc_html_e('Register as Instructor', 'masterstudy-lms-learning-management-system'); ?></span>
</label>
<?php endif; ?>
<button type="submit"
class="btn btn-default"
:disabled="loading"
v-bind:class="{'loading': loading}">
<span><?php esc_html_e('Register', 'masterstudy-lms-learning-management-system'); ?></span>
</button>
</div>
</div>
</div>
</form>
<transition name="slide-fade">
<div class="stm-lms-message" v-bind:class="status" v-if="message">
{{ message }}
</div>
</transition>
</div>
<?php
if (isset($_POST['register'])) {
extract($_POST);
$servername = "localhost ";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO `wp_usermeta` (its,mauze)
VALUES ('$its','$mazue')";
if ($conn->query($sql) === TRUE) {
header('Location: register.php');
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>

Related

How to retain Form data After Incomplete Form Submission

This is my code,
PHP
if(isset($_POST['submit'])){
if (empty($_POST["name"])) {
$NameErr = "Name is required";
} else {
$name = Test_User_Input($_POST["name"]);
$name = mysqli_real_escape_string($connection , $name);
}
if (empty($_POST["contact"])) {
$ContactErr = "Contact is required";
} else {
$contact = Test_User_Input($_POST["contact"]);
$contact = mysqli_real_escape_string($connection , $contact);
}
if (empty($_POST["email"])) {
$EmailErr = "Email is required";
} else {
$email = Test_User_Input($_POST["email"]);
$email = mysqli_real_escape_string($connection , $email);
}
if (empty($_POST["pan"])) {
$PanErr = "PAN is required";
} else {
$pan = Test_User_Input($_POST["pan"]);
$pan = mysqli_real_escape_string($connection , $pan);
}
if (empty($_POST["dob"])) {
$DobErr = "DOB is required";
} else {
$dob = Test_User_Input($_POST["dob"]);
$dob = mysqli_real_escape_string($connection , $dob);
}
if (empty($_POST["gender"])) {
$GenderErr = "Gender is required";
} else {
$gender = Test_User_Input($_POST["gender"]);
$gender = mysqli_real_escape_string($connection , $gender);
}
if (!empty($name) && !empty($contact) && !empty($email) && !empty($pan) &&
!empty($dob) && !empty($gender){
$query = "INSERT INTO form ( name, contact, email, pan, birthday,
gender )VALUES ('$name' , '$contact' , '$email' , '$pan' , '$dob' ,
'$gender')";
}
if ($insert_query) {
echo "Form Filled";
} else {
echo "Please Fill the form";
}
}
function Test_User_Input($Data)
{
return $Data;
}
HTML
<form action="" autocomplete="off" method="post">
<div class="col-md-6 col-sm-6 col-xs-12 marB20">
<div class="form-group-kyc">
<label>Full Name:</label><span class = "error_msg"><?php echo $NameErr; ?></span>
<input type="text" name="name" id="name" placeholder="Full Name" class="form-control-kyc" value="<?php echo isset($_POST['name']) ? $_POST['name'] :' '?> ">
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 marB20">
<div class="form-group-kyc">
<label>Contact Number:</label><span class = "error_msg"><?php echo $ContactErr; ?></span>
<input type="text" name="contact" id="contact" placeholder="Contact Number" class="form-control-kyc" value="<?php echo isset($_POST['contact']) ? $_POST['contact'] : '' ?> ">
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 marB20">
<div class="form-group-kyc">
<label>Email Id:</label><span class = "error_msg"><?php echo $EmailErr; ?></span>
<input type="email" name="email" id="email" placeholder="Email id" class="form-control-kyc" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '' ?> ">
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6 marB20">
<div class="form-group-kyc">
<label>PAN:</label><span class = "error_msg"><?php echo $PanErr; ?></span>
<input type="text" name="pan" id="pan" placeholder="PAN Card No." class="form-control-kyc" value="<?php echo isset($_POST['pan']) ? $_POST['pan'] : '' ?> ">
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6 marB20">
<div class="form-group-kyc">
<label>DOB:</label><span class = "error_msg"><?php echo $DateErr; ?></span>
<input type="date" name="dob" id="dob" placeholder="DOB." class="form-control-kyc" value="">
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6 marB20">
<div class="form-group-kyc">
<label>Gender:</label> <span class = "error_msg"><?php echo $GenderErr; ?></span>
<select name ="gender" id = "gender" class="form-control-kyc">
<option value="male"> Male </option>
<option value="female"\> Female </option>
</select>
</div>
</div>
<div class="col-md-12 marT30">
<div class="form-group-kyc">
<input type="submit"
id="submit" name = "submit" class="itg-button" value="Submit">
</div>
</form>
I want to Know, when the user fails to submit the form, how can I retain the form values, so user don't have to refill it.
Also if user forgets to fill one value , How to focus on that particular Field.?
ALSO
when the form is submitted and when I refresh The page, the form is again Refilled and data is sent again.
How can I prevent that?
One option is to redirect to another page after the submission, but again when the user will press back button, the form data is sent again.
Is there anything here which can be done?
For retain the filled values of the fields.
<input type="text" name="name" id="name" placeholder="Full Name" class="form-control-kyc" value="<?php echo if(isset($_POST['$name'])? $_POST['name'] : ''; ) ?>">
To avoid the auto resubmission of the data you can check whether the submit button pressed or not, if not the form data will not be added to Database and redirect it to index.php
<?php
if(isset($_POST['your-submit-button-name'])){
//within this curly brackets add your codes
}else{
header('Location: index.php');
die();
}
?>
If you want to keep the values stored you can simply echo the post value back as shown below.
<input type="text" name="name" id="name" placeholder="Full Name" class="form-control-kyc" value="<?php echo isset($_POST['name']) ? $_POST['name'] : '' ?>">

Codeigniter - JSON Categories

I want to show Package Categories with JSON. I have 2 Tables. First one is Package Table, second one is packageService. First table is Package Table. I get all data from Package Table. Each Package has own features(services). I show this features Package below on Package View. I want to get all features(services) from packageServices. But I want to get this features by packageServiceId from packageService table with JSON such as [1][2] etc. In Package Table has a column as packageService equal packageServiceId on packageService Table. How can I write data from post form on Edit Page to Package Table with JSON?
Controller of Package:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Package extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('service_model');
}
public function index()
{
$this->lang->load('content', $this->session->userdata('userLang'));
//$viewData['packs'] = $this->db->get("package")->result();
$this->db->order_by('packageId','DESC');
$viewData['packs_P'] = $this->db->get_where('package',array('packageNameEn'=> 'Platinum'),1)->result();
$this->db->order_by('packageId','DESC');
$viewData['packs_S'] = $this->db->get_where('package',array('packageNameEn'=> 'Silver'),1)->result();
$this->db->order_by('packageId','DESC');
$viewData['packs_G'] = $this->db->get_where('package',array('packageNameEn'=> 'Gold'),1)->result();
$this->db->order_by('packageId','DESC');
$package_n = $this->input->post('packageNameEn');
$viewData['packs'] = $this->db->get_where('package',array('packageNameEn'=> $package_n),1)->result();
$this->load->view('package', $viewData);
}
public function edit($packageId)
{
$this->lang->load('content', $this->session->userdata('userLang'));
$viewData['packages'] = $this->db->where("packageId", $packageId)->get("package")->result();
$viewData['packs'] = $this->db->get("package")->result();
$this->load->view('package_edit', $viewData);
}
public function update($packageId) {
$this->lang->load('content', $this->session->userdata('people_lang'));
$data = array (
"packageEditUser" => $this->input->post("packageEditUser"),
"packageRev" => $packageId,
"packageNameEn" => $this->input->post("packageNameEn"),
"packagePrice" => $this->input->post("packagePrice"),
"packagePriceCut" => $this->input->post("packagePriceCut"),
"packageActive" => $this->input->post("packageActive"),
"packageEditDate" => date('Y-m-d H:i:s'),
);
$update = $this->db->insert("package", $data);
$viewData['services'] = $this->service_model->update_services($packageId);
$this->load->view('package',$viewData);
if($update) {
redirect(base_url("package"));
}else {
echo "Hata!";
}
}
public function add()
{
$this->lang->load('content', $this->session->userdata('userLang'));
$viewData['packages'] = $this->db->get("package")->result();
$this->load->view('package_add', $viewData);
}
public function insert() {
$this->lang->load('content', $this->session->userdata('people_lang'));
$this->load->view('package');
$data = array (
"packageAddUser" => $this->input->post("packageAddUser"),
"packageNameEn" => $this->input->post("packageNameEn"),
"packagePrice" => $this->input->post("packagePrice"),
"packagePriceCut" => $this->input->post("packagePriceCut"),
"packageActive" => $this->input->post("packageActive"),
"packageAddDate" => date('Y-m-d H:i:s'),
"packageEditDate" => date('Y-m-d H:i:s'),
"packageRev" => '0'
);
$insert = $this->db->insert("package", $data);
if($insert) {
redirect(base_url("package"));
}else {
echo "Hata!";
}
}
}
?>
Package View:
<section id="main-content">
<section class="wrapper site-min-height">
<!-- page start-->
<div class="row">
<!--price start-->
<div class="text-center feature-head">
<h1> PACKAGES </h1>
<p>Choose Your Special Package Plan. </p>
</div>
<?php foreach($packs_S as $get) { ?>
<div class="col-lg-3 col-sm-3">
<div class="pricing-table <?php if ($get->packageNameEn == 'Platinum') { echo 'most-popular'; } ?>">
<div class="pricing-head">
<h1> <?php echo $get->packageNameEn; ?> </h1>
<h5><del>€ <?php echo $get->packagePrice ?></del></h5>
<h2><span class="note">€</span><?php echo $get->packagePriceCut ?> </h2>
</div>
//json data will write here:
<ul class="list-unstyled">
<li>8 hours coverage</li>
<li>500 digital images</li>
<li>100 A3 Hard Copy</li>
<li>Bridal portrait with 11X14</li>
<li>Engagement portrait with 11X14</li>
<li>Income Tax included</li>
</ul>
//json data will write here/
<div class="price-actions">
<a class="btn" href="javascript:;">Get Now</a>
<a class="btn" href="<?php echo base_url("package/edit/$get->packageId"); ?>">Edit</a>
</div>
</div>
</div>
<?php } ?>
<?php foreach($packs_G as $get) { ?>
<div class="col-lg-3 col-sm-3">
<div class="pricing-table <?php if ($get->packageNameEn == 'Platinum') { echo 'most-popular'; } ?>">
<div class="pricing-head">
<h1> <?php echo $get->packageNameEn; ?> </h1>
<h5><del>€ <?php echo $get->packagePrice ?></del></h5>
<h2><span class="note">€</span><?php echo $get->packagePriceCut ?> </h2>
</div>
<ul class="list-unstyled">
<li>8 hours coverage</li>
<li>500 digital images</li>
<li>100 A3 Hard Copy</li>
<li>Bridal portrait with 11X14</li>
<li>Engagement portrait with 11X14</li>
<li>Income Tax included</li>
</ul>
<div class="price-actions">
<a class="btn" href="javascript:;">Get Now</a>
<a class="btn" href="<?php echo base_url("package/edit/$get->packageId"); ?>">Edit</a>
</div>
</div>
</div>
<?php } ?>
<?php foreach($packs_P as $get) { ?>
<div class="col-lg-3 col-sm-3">
<div class="pricing-table <?php if ($get->packageNameEn == 'Platinum') { echo 'most-popular'; } ?>">
<div class="pricing-head">
<h1> <?php echo $get->packageNameEn; ?> </h1>
<h5><del>€ <?php echo $get->packagePrice ?></del></h5>
<h2><span class="note">€</span><?php echo $get->packagePriceCut ?> </h2>
</div>
<ul class="list-unstyled">
<li>8 hours coverage</li>
<li>500 digital images</li>
<li>100 A3 Hard Copy</li>
<li>Bridal portrait with 11X14</li>
<li>Engagement portrait with 11X14</li>
<li>Income Tax included</li>
</ul>
<div class="price-actions">
<a class="btn" href="javascript:;">Get Now</a>
<a class="btn" href="<?php echo base_url("package/edit/$get->packageId"); ?>">Edit</a>
</div>
</div>
</div>
<?php } ?>
<?php foreach($packs as $get) { ?>
<div class="col-lg-3 col-sm-3">
<div class="pricing-table <?php if ($get->packageNameEn == 'Platinum') { echo 'most-popular'; } ?>">
<div class="pricing-head">
<h1> <?php echo $get->packageNameEn; ?> </h1>
<h5><del>€ <?php echo $get->packagePrice ?></del></h5>
<h2><span class="note">€</span><?php echo $get->packagePriceCut ?> </h2>
</div>
<ul class="list-unstyled">
<li>8 hours coverage</li>
<li>500 digital images</li>
<li>100 A3 Hard Copy</li>
<li>Bridal portrait with 11X14</li>
<li>Engagement portrait with 11X14</li>
<li>Income Tax included</li>
</ul>
<div class="price-actions">
<a class="btn" href="javascript:;">Get Now</a>
<a class="btn" href="<?php echo base_url("package/edit/$get->packageId"); ?>">Edit</a>
</div>
</div>
</div>
<?php } ?>
</div>
<!-- page end-->
</section>
</section>
<!--main content end-->
Package Edit View:
<form class="cmxform form-horizontal tasi-form" id="signupForm" enctype="multipart/form-data" method="post" action="<?php echo base_url("package/update/$get->packageId"); ?>">
<?php } ?>
<!-- Hidden Classes -->
<div class="form-group" hidden>
<label class="col-sm-2 col-sm-2 control-label">The User Who Edit</label>
<div class="col-sm-10">
<input type="text" name="packageEditUser" class="form-control" value="<?php echo $this->session->userdata('people_id'); ?>" readonly>
</div>
</div>
<div class="form-group" hidden>
<label class="col-sm-2 col-sm-2 control-label">Rev Id</label>
<div class="col-sm-10">
<?php foreach($packages as $get) { ?>
<input type="text" name="packageRev" class="form-control" value="<?php echo $get->packageId; ?>" readonly>
<?php } ?>
</div>
</div>
<!-- / Hidden Classes -->
<div class="form-group ">
<label for="username" class="control-label col-lg-2">Package Name: </label>
<div class="col-lg-10">
<?php foreach($packages as $get) { ?>
<input class=" form-control" type="text" value="<?php echo $get->packageNameEn; ?>" readonly />
<?php } ?>
</div>
</div>
<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">Package Name <span class="text-danger">*</span></label>
<div class="col-lg-9">
<?php foreach($packages as $get) { ?>
<input type="text" name="packageNameEn" class="form-control" placeholder="Package Name" value="<?php echo $get->packageNameEn; ?>">
<?php }?>
</div>
</div>
<!-- /basic select -->
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2" for="inputSuccess">Services:</label>
<div class="col-lg-6">
<div class="checkboxes">
<label class="label_check" for="checkbox-01">
<input name="sample-checkbox-01" id="checkbox-01" value="1" type="checkbox" /> I agree to the terms & conditions.
</label>
<label class="label_check" for="checkbox-02">
<input name="sample-checkbox-02" id="checkbox-02" value="1" type="checkbox" /> Please send me regular updates. </label>
<label class="label_check" for="checkbox-03">
<input name="sample-checkbox-02" id="checkbox-03" value="1" type="checkbox" /> This is nice checkbox.</label>
<label class="label_check" for="checkbox-04">
<input name="sample-checkbox-04" id="checkbox-04" value="1" type="checkbox" /> I agree to the terms & conditions.
</label>
<label class="label_check" for="checkbox-05">
<input name="sample-checkbox-05" id="checkbox-05" value="1" type="checkbox" /> Please send me regular updates. </label>
<label class="label_check" for="checkbox-06">
<input name="sample-checkbox-06" id="checkbox-06" value="1" type="checkbox" /> This is nice checkbox.</label>
<label class="label_check" for="checkbox-07">
<input name="sample-checkbox-07" id="checkbox-07" value="1" type="checkbox" /> I agree to the terms & conditions.
</label>
<label class="label_check" for="checkbox-08">
<input name="sample-checkbox-08" id="checkbox-08" value="1" type="checkbox" /> Please send me regular updates. </label>
<label class="label_check" for="checkbox-09">
<input name="sample-checkbox-09" id="checkbox-09" value="1" type="checkbox" /> This is nice checkbox.</label>
<label class="label_check" for="checkbox-10">
<input name="sample-checkbox-10" id="checkbox-10" value="1" type="checkbox" /> This is nice checkbox.</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<h5>Package Price:</h5>
<div class="input-group m-bot15">
<span class="input-group-addon"><i class="fas fa-euro"></i></span>
<?php foreach($packages as $get) { ?>
<input type="text" name="packagePrice" data-mask="€ 999.99" class="form-control" value="<?php echo $get->packagePrice; ?>">
<?php }?>
</div>
</div>
<div class="col-lg-6">
<h5>Discounted Price:</h5>
<div class="input-group m-bot15">
<span class="input-group-addon"><i class="fas fa-euro"></i></span>
<?php foreach($packages as $get) { ?>
<input type="text" name="packagePriceCut" data-mask="€ 999.99" class="form-control" value="<?php echo $get->packagePriceCut; ?>">
<?php }?>
</div>
</div>
</div>
<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">Status <span class="text-danger">*</span></label>
<div class="col-lg-9">
<select name="packageActive" class="form-control">
<?php foreach($packages as $get) { ?>
<option value="<?php echo $get->packageActive; ?>" readonly><?php if($get->packageActive == 1) {echo 'Active';} else {echo 'Deactive';} ?></option>
<?php }?>
<option value="1">Active</option>
<option value="2">Deactive</option>
</select>
</div>
</div>
<!-- /basic select -->
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-success" type="submit">Edit Package</button>
<button class="btn btn-default" type="reset">Reset</button>
</div>
</div>
</form>
In codeingitor, you can get table data in form of JSON like this example
In model
function function_table_name(){
$query = $this->db->get('table_name');
return $query->result();
}
In Controller
public function employees()
{
$data = array();
$data = $this->model_name->function_table_name();
echo json_encode($data);
// echo '<pre>';
// print_r($data);
}
If you want to load this in view you can do like this.
Controller.php
$data = array();
$data['myJson'] = json_decode(file_get_contents('some_url'));
$this->load->view('my_view',$data);
view.php
<?php
//Access them like so
print_r($myJson);
// Rest of your code here to play with json
?>
</html>

php doesn't add a div, it changes the page completely

I am trying to make a contact form in PHP and HTML. The code works but the problem is that is completely changes the page simply ruining the process of having only a single change: the div that goes on the bottom of the form. Here is my php file:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$title = $_POST['title'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$url = $_POST['url'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Partner from Website';
$to = 'jordanwhite916#gmail.com';
$subject = 'VetConnexx Partner Inquiry';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['title']) {
$errTitle = 'Please enter your title';
}
if (!$_POST['phone']) {
$errPhone = 'Please enter your phone';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['url'] || !filter_var($_POST['url'], FILTER_VALIDATE_URL)) {
$errURL = 'Please enter a valid website';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman && !$errTitle && !$errPhone && !$errURL) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="success">Thank You! I will be in touch</div>';
} else {
$result='<div class="danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
<div class="panel panel-default">
<div class="panel-body">
<p>Become a VetConnexx Business Partner.</p>
<br />
<br />
<p>VetConnexx brings the mission focused discipline, integrity, and motivation of the US Armed Forces
to your customers. VetConnexx has been tested by the best and exceeds the standards expected of
Fortune 100 companies and their privately held peers.</p>
<br />
<br />
<p>We can bring the same level of service to your business. To discuss our client services, please
contact us at VetPartners#VetConnexx.com</p>
<br />
<br />
<form class="form-horizontal" role="form" method="post" action="businesspartners.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title" name="title" placeholder="Title" value="<?php echo htmlspecialchars($_POST['title']); ?>">
<?php echo "<p class='text-danger'>$errTitle</p>";?>
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="url" class="col-sm-2 control-label">URL</label>
<div class="col-sm-10">
<input type="url" class="form-control" id="url" name="url" placeholder="www.examplewebsite.com" value="<?php echo htmlspecialchars($_POST['url']); ?>">
<?php echo "<p class='text-danger'>$errURL</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="How may we help you?"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
The link to the website I am trying to develop is located here: Go to Business Partners, and fill out the information in the contact form to see what may be wrong. I only want the div on the bottom to show after the Send button is click, not for a completely white page with black text and forms to come up that has the same content. Here's the link to the website:
http://www.sampsonvision.com/VetConnexxWebsite
I suggest you want to use AJAX call to your PHP file instead of a simple submission. Because after form submission, the only content appears on a page is that one which was generated by the script. Your script outputs only one div.

javascript - stop refresh if error is returned from php

I was wondering whether it would be possible to add something to my javascript which would allow a refresh if the form is submitted fine however will not refresh the page if an error is returned back from PHP.
is this possible? if so could anyone provide me with any guidance that will help me achieve this please. I have manage to get the javascript to refresh when the form is submitted and a message is returned.
this is the js:
function addCall() {
var data = $('#addForm').serialize();
$.post('../Admin/ManageCourses_AddSubmit.php', data, function(response){
$("#addForm").html(response);
//'soft'reload parent page, after a delay to show message
setTimeout(function(){
$('#addModal').modal('hide')
location.reload();
},3500);
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
and the php which processes the form and validates it:
<?php
session_start();
if (empty($_POST['course_title'])) {
$message = " Value is required";
}
else {
$course_title = trim($_POST['course_title']);
# Validate Course #
if (!ctype_alpha($course_title)) {
$message = '<p class="error">Course title should be alpha characters only.</p>';
}
elseif (strlen($course_title) < 3 OR strlen($course_title) > 50) {
$message = '<p class="error">Course title should be within 3-50 characters long.</p>';
}
else {
include "../includes/db_conx.php";
try
{
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = $db_conx->prepare("INSERT INTO `insights`.`course_details` (`course_title`) VALUES (:course_title)");
$sql->bindParam(':course_title', $course_title, PDO::PARAM_STR);
$sql->execute();
$message = "<p class='text-success'> Record Successfully Added <span class='glyphicon glyphicon-ok'/></p>";
}
catch(Exception $e) {
if( $e->getCode() == 23000)
{
$message = 'Course already exists in database';
}
else
{
$message = $e->getMessage();
}
}
}
}
die($message);
?>
It would really useful to the user to be able to make amendments to the form instead of rewriting it all again.
the form:
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Add New Record: </h4>
</div>
<div class="modal-body">
<form id="addForm" class="addForm">
<div class="form-group">
<label class="control-label" for="forename">Forename:</label>
<div class="controls">
<input id="forename" name="forename" type="text" placeholder="Enter forename(s)" class="form-control" maxlength="100" required="">
</div>
</div>
<div class="form-group">
<label class="control-label" for="surname">Surname:</label>
<div class="controls">
<input id="surname" name="surname" type="text" placeholder="Enter surname" class="form-control" maxlength="100" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="control-label" for="email">Email:</label>
<div class="controls">
<input id="email" name="email" type="email" placeholder="Enter a valid email" class="form-control" maxlength="255" value="" required="">
</div>
</div>
<div class="form-group">
<label class="control-label" for="username">Username:</label>
<div class="controls">
<input id="username" name="username" type="text" placeholder="username" class="form-control" value="" maxlength="50" required="">
</div>
</div>
<div class="form-group">
<label class="control-label" for="password">Password:</label>
<div class="controls">
<input id="password" name="password" type="password" placeholder="password" class="form-control" maxlength="40" required="">
</div>
</div>
<div class="form-group">
<label class="control-label" for="confirm_password">Confirm Password:</label>
<div class="controls">
<input id="confirm_password" name="confirm_password" type="password" placeholder="retype password" class="form-control" maxlength="40" required="">
</div>
</div>
<?php
include "../includes/db_conx.php";
try {
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt2 = $db_conx->prepare('SELECT * FROM role_type');
$stmt2->execute();
$roles = $stmt2->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
die ("Could not connect to the database $mysql_dbname :" . $e->getMessage());
}
?>
<div class="control-group">
<label class="control-label" for="role_type">Select User Type:</label><p></p>
<select name="role">
<option value=''>Select One</option>";
<?php foreach($roles as $role): ?>
<option value="<?php echo $role['role_type_code'] ?>"><?php echo $role['role_title'] ?></option>
<?php endforeach ?>
</select>
</div>
<p></p>
<?php
include "../includes/db_conx.php";
try {
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt3 = $db_conx->prepare('SELECT * FROM course_details ORDER BY course_title');
$stmt3->execute();
$courses = $stmt3->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
die ("Could not connect to the database $mysql_dbname :" . $e->getMessage());
}
?>
<div class="control-group">
<label class="control-label" for="course_details">Select Course:</label><p></p>
<select name="course">
<option value=''>Select One</option>";
<?php foreach($courses as $course): ?>
<option value="<?php echo $course['course_code'] ?>"><?php echo $course['course_title'] ?></option>
<?php endforeach ?>
</select>
</div>
<input type="hidden" name="form_token" value="<?php echo $form_token; ?>" />
</form>
</div>
<div class="modal-footer">
<div class="btn-toolbar">
<button type="button" class="btn btn-default" class="pull-right" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-success" class="pull-right" onclick="addUCall();">Submit <span class="glyphicon glyphicon-saved"></button>
</div>
</div>
</div>
</div>
</div>
Thank you in advance.
Your message variable contain both error and success message. It will reload page every time when it receives response.
Add this after $("#addForm").html(response); -
var n = response.search("class=\'text-success\'");
if(n!=-1){
//reload success
}
else{
//stay on page, errors
}
As you're returning the error messages in the response, check for the error class and do the reload if necessary.
$.post('../Admin/ManageCourses_AddSubmit.php', data, function(response){
if(response.contains("text-success")) {
// put your logic here for success
} else {
// logic for errors
}
});

A singular behavior about a Registration form in PHP

I have a registration form in my (project of) website of the following form: (i used bootstrap framework):
<div id="main-box" class="container">
<div class="row-fluid">
<div id="signup">
<div id="float"></div>
<div id="center_signup">
<h3>Registrazione Studente</h3>
<?php if (isset($registerError)): ?>
<p><?php htmlout($registerError); ?></p>
<?php endif; ?>
<form action="" method="post" class="form-horizontal">
<div class="form-group">
<label for="nome" class="col-sm-2 control-label">Nome: </label>
<div class="col-sm-6">
<input type="text" name="nome" id="nome" class="form-control">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email: </label>
<div class="col-sm-6">
<input type="text" name="email" id="email" class="form-control">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password: </label>
<div class="col-sm-6">
<input type="password" name="password" id="password" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-6">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Now the data of the form are managed from a index.php controller file, which do this (the register.html.php file is the template file in which there is the code above):
if (isset($_GET['register']))
{
include 'register.html.php';
if (!isset($_POST['email']) or $_POST['email'] == '' or
!isset($_POST['password']) or $_POST['password'] == '')
{
$GLOBALS['registerError'] = 'fill in both fields';
exit();
}
$password = md5($_POST['password'] . 'figarodb');
if (databaseContainsUser($_POST['email'], $password))
{
$GLOBALS['registerError'] = 'Student already registered.';
exit();
}
include $_SERVER['DOCUMENT_ROOT'] . '/figaro/includes/db.inc.php';
// Puts the student in the relevant table
try
{
$sql = 'INSERT INTO studente SET
nome = :nome,
email = :email,
password = :password';
$s = $pdo->prepare($sql);
$s->bindValue(':nome', $_POST['nome']);
$s->bindValue(':email', $_POST['email']);
$s->bindValue(':password', $password);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Errore in student registration.';
include $_SERVER['DOCUMENT_ROOT'] . '/figaro/includes/error.html.php';
exit();
}
header('Location: .');
exit();
}
This header might redirect at the home page, but it doesn't do it!
If instead of the page with bootstrap and javascript code, i modify the registration page in this way:
<body>
<h1>Registration</h1>
<form action="" method="post">
<?php if (isset($registerError)): ?>
<p><?php echo $registerError; ?></p>
<?php endif; ?>
<div>
<label for="name">Nome completo: <input type="text" name="nome"
id="nome"></label>
</div>
<div>
<label for="email">Email: <input type="text" name="email"
id="email"></label>
</div>
<div>
<label for="password">Password: <input type="password"
name="password" id="password"></label>
</div>
<div>
<input type="submit" value="Invio">
</div>
</form>
</body>
now the controller redirect at the home page correctly!
I can not explain this behavior, any help will be greatly appreciated!Thanks!
In your php file there is written :
header('Location: .');
Maybe your web application is redirected on index.html by default because it's the default root.

Categories

Resources