Codeigniter - Forms with multiple dropzones for Picture Gallery & PDF files - php

Hi I'm working on a real estate project. I have a form which has some inputs & then 3 forms incorporated in the 1st form which have the dropzone class for a simple picture, a picture gallery & pdf files. I tried a lot of techniques but couldn't get the dropzones to work with the main form.
My Issues:
Dropzone sends the inputs & picture before submit button gets hit.
If after that, the submit button gets clicked then some inputs get null & 2 records get inserted.
If I remove or hide the submit button or change it to anchor tag for redirecting then the picture doesn't get uploaded.
Listings_controller:
function insert_listing(){
$pass = rand(0,999);
$data_seller = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'cell' => $this->input->post('cell'),
'sms' => $this->input->post('sms'),
'pass' => MD5($pass),
'status' => 'seller',
'last_date' => date('Y-m-d')
);
$result1 = $this->users_model->add_seller($data_seller);
$data_listing = array(
'code' => $this->input->post('code'),
'address' => $this->input->post('address'),
'city' => $this->input->post('city'),
'state' => $this->input->post('state'),
'zipcode' => $this->input->post('zipcode'),
'process' => $this->input->post('process'),
'step' => $this->input->post('step'),
'price' => $this->input->post('price'),
'agent_id' => $this->input->post('agent'),
'seller_id' => $this->db->insert_id()
);
$result2 = $this->listings_model->add_listing($data_listing);
if($result2){
$this->session->set_flashdata('success','Listing Added Successfully');
}else{
$this->session->set_flashdata('error','Listing Already Exists !');
}
//Dropzone - Pic
if(!empty($_FILES)){
$tempFile = $_FILES['file']['tmp_name'];
$fileName = substr(sha1(rand(000,9999)),0,7).$_FILES['file']['name'];
$targetPath = getcwd().'/assets/admin/images/image-gallery/';
$targetFile = $targetPath.$fileName;
move_uploaded_file($tempFile,$targetFile);
$listing_id = $result2['id'];
$data_listing_pic['listing_id'] = $listing_id;
$data_listing_pic['pic'] = $fileName;
$result3 = $this->listings_model->add_listing_pic($data_listing_pic);
}
redirect('admin/listings');
}
Users_model:
//Seller Add
function add_seller($data_seller){
//Check Duplicate
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('email',$data_seller['email']);
$query = $this->db->get();
if($query->num_rows() > 0){
$seller_id = $query->result_array();
$data_seller['id'] = $seller_id[0]['id'];
//Update Info
$this->db->where('email',$data_seller['email']);
$this->db->update($this->table,$data_seller);
return $data_seller;
}else{
//INSERT Seller
$result = $this->db->insert($this->table,$data_seller);
//GET Last Inserted ID
$seller_id = $this->db->insert_id();
$data_seller['id'] = $seller_id;
return $data_seller;
}
}
Listings_model:
//Listing Add
function add_listing($data_listing){
$result = $this->db->insert($this->table,$data_listing);
$listing_id = $this->db->insert_id();
$data_listing['id'] = $listing_id;
return $data_listing;
}
//Listing Pic Add
function add_listing_pic($data_listing_pic){
$result = $this->db->insert($this->pics_table,$data_listing_pic);
return $result;
}
My View:
<form action="<?php echo base_url();?>admin/insert_listing" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<input type="hidden" name="code" value="<?php echo $listing_code; ?>" />
<div class="row">
<div class="col-lg-6">
<strong>Property Information:</strong>
<br><br>
<div class="form-group form-float form-group-lg">
<div class="form-line">
<input type="text" class="form-control" name="address" autofocus required />
<label class="form-label">Street Address:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="city" required />
<label class="form-label">City/Town:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="state" required />
<label class="form-label">State:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="zipcode" required />
<label class="form-label">Zip Code:</label>
</div>
</div>
</div>
<div class="col-lg-6">
<strong>Seller Information:</strong>
<br><br>
<div class="form-group form-float form-group-lg">
<div class="form-line">
<input type="text" class="form-control" name="name" required />
<label class="form-label">Full Name(s):</label>
</div>
<br>
<div class="form-line">
<input type="email" class="form-control" name="email" required />
<label class="form-label">Seller's Email:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="cell" required />
<label class="form-label">Cell Phone Number:</label>
</div>
<br>
<div class="demo-checkbox">
<input type="checkbox" id="md_checkbox_26" name="sms" class="filled-in chk-col-blue" checked />
<label for="md_checkbox_26">Send status updates via SMS</label>
</div>
</div>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-6">
<strong>Listing Agent:</strong>
<br /><br />
<select class="form-control show-tick" name="agent" required >
<option value="">-- Please choose from the drop down --</option>
<?php for($j=0; $j < count($agent_list); $j++){ ?>
<option value="<?php echo $agent_list[$j]['id']; ?>" ><?php echo $agent_list[$j]['name']; ?></option>
<?php } ?>
</select>
<br />
</div>
<div class="col-lg-6">
<strong>Listing Status Information:</strong>
<br /><br />
<select class="form-control show-tick" name="process" required >
<option value="">-- Please choose from the drop down --</option>
<option value="Listing Process">Listing Process</option>
</select>
<br /><br />
<select class="form-control show-tick" name="step" required >
<option value="">-- Please choose from the drop down --</option>
<option value="1">Step 1: The Prep</option>
<option value="2">Step 2: File Set Up</option>
<option value="3">Step 3: Pre-listing Checklists</option>
<option value="4">Step 4: Sign Installation</option>
<option value="5">Step 5: Feature Sheets</option>
<option value="6">Step 6: Open Houses</option>
<option value="7">Step 7: You Received an Offer!</option>
</select>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-12">
<strong>Photo and Price:</strong>
<br><br>
<div class="form-group form-float form-group-lg">
<div class="form-line">
<input type="text" class="form-control" name="price" required />
<label class="form-label">Listing Price: $</label>
</div>
</div>
<form action="<?php echo base_url();?>admin/insert_listing_pdf" id="PDFUpload" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<div class="dz-message">
<div class="drag-icon-cph">
<i class="material-icons">touch_app</i>
</div>
<h3>Drop the Photo here or Click to Upload.</h3>
<em>(It is recommended to upload the most attractive photo that will catch the user's eye)</em>
</div>
</form>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-6">
<strong>Upload Gallery Photos:</strong>
<br><br>
<form action="<?php echo base_url();?>admin/insert_listing_gallery" id="GalleryUpload" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<div class="dz-message">
<div class="drag-icon-cph">
<i class="material-icons">touch_app</i>
</div>
<h3>Drop files here or click to upload.</h3>
<em>(Upload all the gallery photos of the property in this section)</em>
</div>
</form>
</div>
<div class="col-lg-6">
<strong>Upload PDF Files:</strong>
<br><br>
<form action="<?php echo base_url();?>admin/insert_listing_pdf" id="PDFUpload" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<div class="dz-message">
<div class="drag-icon-cph">
<i class="material-icons">touch_app</i>
</div>
<h3>Drop files here or click to upload.</h3>
<em>(Upload all the PDF files related to the property in this section)</em>
</div>
</form>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-12">
<button type="submit" class="btn btn-primary btn-lg waves-effect">ADD LISTING</button>
</div>
</div>
</form>

Related

How to change ConvForm value to string

I need to take value given by user and change it into php $weight
I tried:
1. giving an input attribute: inputIdName="weight"
2. Many ways to take this value
<input type="text" name="weight" data-conv-question="Your weight?">
<input type="text" data-conv-question="Your weight is {weight}:0." data-no-answer="true">
Full Code
section id="demo">
<div class="vertical-align">
<div class="container">
<div class="row">
<div class="col-sm-6 col-sm-offset-3 col-xs-offset-0">
<div class="card no-border">
<div id="chat" class="conv-form-wrapper">
<form action="answer.php" method="GET" class="hidden">
<select data-conv-question="Witaj, pomóc Ci w wyborze idealnego produktu?" data-callback="storeState" name="pytanie">
<option value="tak">Tak</option>
<option value="nie" data-callback="restore">Może później..</option>
</select>
<div data-conv-fork="pytanie">
<div data-conv-case="tak">
<input type="text" name="produkt" data-conv-question="Wporządku, jaki produkt Cię interesuje?">
</div>
<div data-conv-case="nie">
<select data-conv-question="Okej! Jakby co wiesz gdzie mnie szukać ;)" id="" name="pytanie2">
<option value="back" data-callback="rollback">Dzięki!</option>
<option value="no" data-callback="restore">Jednak potrzebuję pomocy</option>
</select>
</div>
<div data-conv-fork="pytanie2">
<div data-conv-case="no">
<input type="text" name="produkt" data-conv-question="Wporządku, jaki produkt Cię interesuje?">
</div>
</div>
</div>
<input type="text" data-conv-question="Okej, interesuje Cię {produkt}:0! " value="kategoria" name="kategoria" inputIdName="kategoria" data-no-answer="true">
<input type="text" data-conv-question="Odpowiedz teraz na kilka pytań, które pomogą nam wybrać idealny sprzęt dla Ciebie" data-no-answer="true">
<input type="text" name="wzrost" data-conv-question="Jakiego jesteś wzrostu?">
<input type="text" data-conv-question="Więc mierzysz {wzrost}:0." data-no-answer="true">
<input type="text" name="waga" data-conv-question="Ile ważysz?">
<input type="text" data-conv-question="Ważysz {waga}:0." data-no-answer="true">

Load a specific div on id base codeigniter

myiamge
I have 2 forms as tab in signup page when one form is submit and if have any error I want to redirect to that specific tab which have error but did not happen.
Here is my view
<?php include 'header.php'; ?>
<section class="services">
<div class="container">
<div class="signup-wrapper well" style="margin-bottom: 80px; margin-top: 80px;">
<div class="container">
<div class="services__main">
<h2 class="title title--main"><span class="title__bold">Sign Up</span><span class="line line--title"><span class="line__first"></span><span class="line__second"></span></span></h2>
</div>
<div class="aside-tabs__links">
Genrel User
Dealership
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc" >
<div class="col-md-5">
<form action="<?= base_url();?>Home/add_user" method="post" >
<!-- class="quick-form" -->
<div class="form-group">
<input type="text" class="form-control" name="fname" placeholder="Full Name" />
</div>
<?php echo form_error('fname'); ?>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" />
</div>
<?php echo form_error('email'); ?>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Password" />
</div>
<?php echo form_error('password'); ?>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone #" />
</div>
<?php echo form_error('phone'); ?>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<?php echo form_error('city'); ?>
<div class="form-group">
<input type="submit" class="btn button button--red button--main pull-right" style="margin-bottom: 10px;" value="Sign Up">
</div>
</form>
</div>
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev" style="display: none;">
<div class="col-xs-5">
<form action="<?= base_url();?>Home/add_dealer" method="post" enctype="multipart/form-data">
<h4>Comapny Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="cname" placeholder="Company Name" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="owner" placeholder="Owner Name" />
</div>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="address" placeholder="Office Location" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Office Phone" />
</div>
<div class="form-group">
<select name="business-type" class="form-control">
<option class="form-group">Products Deal</option>
<option class="form-group">Bikes</option>
<option class="form-group">Accessories</option>
<option class="form-group">Both</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="link" placeholder="social-link(optional)" />
</div>
<div class="form-group">
<button type="button" class="button button--custom--grey button--main btn" id="logo">Upload Logo</button>
<input type="file" class="form-control hidden" id="logo1" name="logo1" /><span id="mylogo">* Format must be jpg,jpeg or png</span>
</div>
<?php echo form_error('logo'); ?>
<div class="form-group">
<textarea class="form-control" name="descrp" placeholder="Description(optional)"></textarea>
</div>
<h4>Primary Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="password" placeholder="Password" />
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn button button--red button--main pull-right" value="Sign Up">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<?php include 'footer.php'; ?>
here is my controller
function add_dealer()
{
//echo "<pre>"; print_r($_FILES);
$config['upload_path'] = 'C:\xampp\htdocs\devilbirds\images\uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name'] = 'TRUE';
$config['overwrite'] = 'FALSE';
$config['wm_text'] = 'Deveil Birds';
$config['wm_type'] = 'text';
$config['wm_font_path'] = 'C:\xampp\htdocs\devilbirds/fonts/fontawesome-webfont.ttf';
$config['wm_font_size'] = '16';
$config['wm_font_color'] = 'aabbcc';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_padding'] = '20';
$this->load->library('upload', $config);
$this->image_lib->watermark();
$abc = $this->upload->do_upload('logo1');
$upload_data = $this->upload->data();
// var_dump($abc);exit();
$file_name = $upload_data['file_name'];
$this->form_validation->set_rules('cname','CName','trim|required');
$this->form_validation->set_rules('logo1','Logo1','trim');
$this->form_validation->set_rules('address','Address','trim');
$this->form_validation->set_rules('phone','Phone','trim|is_unique[bd_dealer.phone]');
$this->form_validation->set_rules('owner','Owner','trim');
$this->form_validation->set_rules('descrp','Descrp','trim');
$this->form_validation->set_rules('business-type','Business-type','trim');
$this->form_validation->set_rules('link','Link','trim');
$this->form_validation->set_rules('email','Email','trim|is_unique[bd_dealer.email]');
$this->form_validation->set_rules('password','Password','trim|min_length[5]|max_length[20]');
$this->form_validation->set_rules('city','City','trim');
if ($this->form_validation->run() == FALSE)
{
$data['error'] = $this->session->set_flashdata('errors');
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url
}
else
{
$userData = array(
'company' => $this->input->post('cname'),
'logo' => $file_name,
'location' => $this->input->post('address'),
'phone' => $this->input->post('phone'),
'name' => $this->input->post('owner'),
'description' => $this->input->post('descrp'),
'business_type' => $this->input->post('business-type'),
'social_link' => $this->input->post('link'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'city' => $this->input->post('city'));
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'user_type' => '2');
//var_dump($userData);exit();
$this->Home_m->add_dealer($userData);
$this->Home_m->users($data);
redirect('Home/login');
}
}
I have two functions for two different forms in tabs. let suppose if i post my dealer form and this form have any issue then it will redirect to the signup page and it shows the pre-active tab which is general user signup. i want to redirect to the div that contain the form or dealer signup. any help will be appreciated , thanks in advance
In your controller when you get error do this.
for form one error
$data['error'] = $this->session->set_flashdata('errors');
$data['div1'] = "div1";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
For form two error
$data['error'] = $this->session->set_flashdata('errors');
$data['div2'] = "div2";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
In your view page
<script>
$(document).ready(function(){
var div1 = "<?php $div1; ?>";
var div2 = "<?php $div2; ?>";
if(div1=='' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='div1' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='' && div2=='div2'){
$("#desc").hide();
$("#rev").show();
}
})
</script>
div1
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc">
Div2
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev">
I hope it will help you.

Why php form not submitting?

I'm developing a script for online admission in a website. Below is php code of the page. The problem is that it's not submitting.
<?php
include ("include/header.php"), include ("include/config.php");
if(isset($_POST['applyAdmission'])) {
$admission_no = $_POST['admission_no'];
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$p_add = $_POST['p_add'];
$c_add = $_POST['c_add'];
$dob = $_POST['dob'];
$education = $_POST['education'];
$mobile = $_POST['mobile_no'];
$course = $_POST['course'];
$subjects = $_POST['subjects'];
$timing = $_POST['timing'];
$filepath_pic = $_FILES['picture']['name'];
$res_move_pic = move_uploaded_file($_FILES['picture']['tmp_name'], "/admission/".$filepath_pic);
$filepath_sign = $_FILES['sign']['name'];
$res_move_sign = move_uploaded_file($_FILES['sign']['tmp_name'], "/admission/".$filepath_sign);
$agree_terms = $_POST['agree_terms'];
$agree_cond = $_POST['agree_cond'];
if ($res_move_pic == 1 && $res_move_sign == 1 ) {
$query = "INSERT into online_admission (f_name, l_name, p_add, c_add, dob, degree, mobile_no, course, subjects, timing, pic, sign, agree_terms, agree_cond, applied_on)
values ('$f_name','$l_name','$p_add','$c_add','$dob','$education','$mobile','$course','$subjects','$timing','$filepath_pic','$filepath_sign','$agree_terms','$agree_cond','now()')";
$res = mysql_query($query) or die("ERROR: Unable to insert into database.");
if ($res == 1) {
header('Location:http://adarshclasses.in/admission_success.php/');
exit();
} else {
header('Location:http://adarshclasses.in/admission_failed.php/');
exit();
}
} else {
echo "Error in updateing profile pic and sign";
}
} else {
//echo "Please submit the form, thanks!";
}
;?>
Everything in form is correct like I added same name in form which i used in $_POST but still it's not working, please help me to fix this issue.
Here is html codes of form:
<form class="form-horizontal" id="admission_form" method="post" action="" enctype="multipart/form-data">
<!--div class="row">
<div class="col-lg-6">
<label for="admission_no"> Admission No. </label>
<input type="hidden" class="form-control" name="admission_no" value="<?php echo $admission_no ;?>" readonly disabled>
</div>
</div--><br>
<div class="row">
<div class="col-lg-6">
<label for="f_name"> First Name <span class="required">*</span> </label>
<input type="text" class="form-control" name="f_name" placeholder="Your first name" value="<?php echo $f_name ;?>" required>
</div>
<div class="col-lg-6">
<label for="l_name"> Last Name <span class="required">*</span></label>
<input type="text" class="form-control" name="l_name" placeholder="Your last name" value="<?php echo $l_name ;?>" required>
</div>
</div><br>
<div class="row">
<div class="col-lg-12">
<label for="p_add"> Permanent Address <span class="required">*</span></label>
<textarea class="form-control" name="p_add" placeholder="Please write your permanent address" value="<?php echo $p_add ;?>" required></textarea>
</div>
</div><br>
<div class="row">
<div class="col-lg-12">
<label for="c_add"> Current Address in Jodhpur <span class="required">*</span></label>
<textarea class="form-control" name="c_add" placeholder="Please write your address where you currently living" value="<?php echo $c_add ;?>" required></textarea>
</div>
</div><br>
<div class="row">
<div class="col-lg-6">
<label for="dob"> Date of birth <span class="required">*</span></label>
<input type="date" class="form-control" name="dob" placeholder="Your date of birth eg:- 25/11/1996" value="<?php echo $dob ;?>" required>
</div>
<div class="col-lg-6">
<label for="education"> Recent passed degree/exam - </label>
<input type="text" class="form-control" name="education" placeholder="for example - BA/ B.Sc etc." value="<?php echo $education ;?>" >
</div>
</div><br>
<div class="row">
<div class="col-lg-6">
<label for="mobile_no"> Mobile Number <span class="required">*</span></label>
<input type="number" class="form-control" name="mobile_no" placeholder="Enter your mobile number, eg - 8384991980" value="<?php echo $mobile_no ;?>" required>
</div>
<div class="col-lg-6">
<label for="course"> Select course <span class="required">*</span> </label>
<select class="form-control" name="course" required>
<option value="none"> --- Select one course --- </option>
<option value="IAS"> IAS </option>
<option value="RAS"> RAS </option>
<option value="Police constable"> Police constable </option>
<option value="SI"> SI </option>
<option value="Railway"> Railway </option>
<option value="REET"> REET </option>
<option value="Teacher"> Teacher </option>
<option value="Patwar"> Patwar </option>
<option value="Bank PO"> Bank PO </option>
<option value="Jr Accountant"> Jr Accountant </option>
<option value="Rajasthan police"> Rajasthan police </option>
<option value="SSC (10+2)"> SSC (10+2) </option>
</select>
</div>
</div><br>
<div class="row">
<div class="col-lg-6">
<label for="subjects"> Subjects - </label>
<input type="text" class="form-control" name="subjects" placeholder="Enter your subject you want to read" value="<?php echo $subjects ;?>" required>
</div>
<div class="col-lg-6">
<label for="timing"> Classes Timing - </label>
<input type="text" class="form-control" name="timing" placeholder="Your preferred time for coaching" value="<?php echo $timing ;?>" required>
</div>
</div><br>
<div class="row">
<div class="col-lg-6">
<label for="picture"> Upload your picture <span class="required">*</span></label>
<input type="file" class="form-control" name="picture" required>
</div>
<div class="col-lg-6">
<label for="sign"> Upload your signature <span class="required">*</span></label>
<input type="file" class="form-control" name="sign" required>
</div>
</div><br>
<div class="row">
<div class="col-md-12">
<input type="checkbox" aria-label="..." name="agree_terms" value="1"> I agree with Rules and Regulations mentioned below.<br>
<input type="checkbox" aria-label="..." name="agree_cond" value="1"> I hearbly declare that Adarsh Classes can use my pictures after my selection for advertising purpose.
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<button type="text" name="submit" class="btn btn-success btn-lg btn-block" name="applyAdmission"> Submit my application form </button>
</div>
</div>
</div>
</form>
The reason behind that in the input type of the HTML Page for the submit you are using <input type="button"
instead of <input type="submit". Use <input type="submit" that's work.
Example:
<input type="submit" name="" value="Submit">
Changed
<button type="text">
to
<button type="submit">
Change
button type="text" to type="button" Or input type ="submit/button"
You need to change this code:
<button type="text" name="submit" class="btn btn-success btn-lg btn-block" name="applyAdmission"> Submit my application form </button>
with below code :
<input type="submit" name="applyAdmission" value="Submit my application form" class="btn btn-success btn-lg btn-block" />
You also need to make sure that your wrote PHP code in same file, otherwise you have to add PHP file name in action tag in below line:
<form class="form-horizontal" id="admission_form" method="post" action="" enctype="multipart/form-data">
You also have some PHP error in your code, so you have to add first line in your PHP code and then fix your PHP Fatal error.
ini_set('display_errors', '1');
I see a little syntax error and I think fixing this will fix your issue.
Change
include ("include/header.php"), include ("include/config.php");
to
include ("include/header.php");
include ("include/config.php");
To show you the syntax error, here is an example:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
include("test.php"), include("someother.php");
The response:
Parse error: syntax error, unexpected ',' in ...\tests\includeTest.php on line 6
Incorrect input type
You should also change your button type.
Change
<button type="text"...
to
<button type="submit"...
Change <button> to <input>. Buttons can work with javascript but with only php button cant work with post data. You can not get POST data by <button>. For this you have to use <input>
Change this
<button type="text" name="submit" class="btn btn-success btn-lg btn-block" name="applyAdmission"> Submit my application form </button>
to
<input type="submit" name="applyAdmission">
Second:
Here is looking syntax error include ("include/header.php"), include ("include/config.php");
PHP requires instructions to be terminated with a semicolon at the end of each statement. Make them seperate by ; not by ,.
include ("include/header.php");
include ("include/config.php");
You can see documentation for more deep information

wrong <select> value when editing php form

I really hope you can help me.
I've done a form, where I can opt if a client I'm adding to the database is "Active or "Inactive", using a dropdown select box.
My code saves all the data correctly to the datbase, but when I want to edit the client, the option displays always as "Active", ignoring the value from the database.
I have 2 files:
edita_clientes.php - the form where I can edit the clients values
salvar_edicao.php - the file that saves the edition.
Here are the codes:
edita_clientes.php:
<?php
#ini_set('display_errors', '1');
error_reporting(E_ALL);
$id = $_GET["id_cliente"];
settype($id, "integer");
mysql_connect("localhost", "root", "");
mysql_select_db("sistema");
$resultado = mysql_query("select * from tabela where id_cliente = $id");
$dados = mysql_fetch_array($resultado);
mysql_close();
?>
<form id="edita_pj" name="edita_pj" method="post" action="salvar_edicao.php">
<input type="hidden" name="id_cliente" id="id_cliente" value="<?php echo $id;?>" />
<div class="box-body">
<div class="form-group">
<label>Razão Social</label>
<input type="text" name="razao" id="razao" class="form-control" value="<?php echo $dados["razao"];?>" />
</div>
<div class="form-group">
<label>Nome Fantasia</label>
<input type="text" name="fantasia" id="fantasia" class="form-control" value="<?php echo $dados["fantasia"];?>" />
</div>
</div>
<div class="box-body">
<div class="form-group">
<label>CNPJ</label>
<input type="text" name="cnpj" id="cnpj" class="form-control" data-inputmask='"mask": "999.999.999-99"' data-mask value="<?php echo $dados["cnpj"];?>">
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>Logradouro</label>
<input type="text" name="logradouro" id="logradouro" class="form-control" value="<?php echo $dados["logradouro"];?>">
</div>
<div class="col-xs-3">
<label>Número</label>
<input type="text" name="numero" id="numero" class="form-control" value="<?php echo $dados["numero"];?>">
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>Bairro</label>
<input type="text" name="bairro" id="bairro" class="form-control" value="<?php echo $dados["bairro"];?>">
</div>
<div class="col-xs-3">
<label>CEP</label>
<input type="text" name="cep" id="cep" class="form-control" data-inputmask='"mask": "99999-999"' data-mask value="<?php echo $dados["cep"];?>">
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-10">
<label>Cidade</label>
<input type="text" name="cidade" id="cidade" class="form-control" value="<?php echo $dados["cidade"];?>">
</div>
<div class="col-xs-2">
<label>UF</label>
<input type="text" name="uf" id="uf" class="form-control" value="<?php echo $dados["uf"];?>">
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>E-mail</label>
<input type="text" name="email" id="email" class="form-control" value="<?php echo $dados["email"];?>">
</div>
<div class="col-xs-3">
<label>Telefone</label>
<input type="text" name="telefone" id="telefone" class="form-control" data-inputmask='"mask": "(99) 9999.9999"' data-mask value="<?php echo $dados["telefone"];?>"/>
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>Contato</label>
<input type="text" name="contato" id="contato" class="form-control" value="<?php echo $dados["contato"];?>">
</div>
<div class="col-xs-3">
<label>Estado</label>
<select class="form-control" name="estado" id="estado" value=""><?php echo $dados["estado"];?>
<option>Ativo</option>
<option>Inativo</option>
</select>
</div>
</div>
<div class="form-group">
<label>Observações</label>
<textarea class="form-control" name="obs" id="obs" rows="6" ><?php echo $dados["obs"];?>
</textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" name="Submit" class="btn btn-primary">Salvar</button>
</div>
</form>
salvar_edicao.php:
<?php
#ini_set('display_errors', '1');
error_reporting(E_ALL);
$razao = $_POST["razao"];
$fantasia = $_POST["fantasia"];
$cnpj = $_POST["cnpj"];
$logradouro = $_POST["logradouro"];
$numero = $_POST["numero"];
$bairro = $_POST["bairro"];
$cep = $_POST["cep"];
$cidade = $_POST["cidade"];
$uf = $_POST["uf"];
$email = $_POST["email"];
$telefone = $_POST["telefone"];
$contato = $_POST["contato"];
$estado = $_POST["estado"];
$obs = $_POST["obs"];
$id = $_POST["id_cliente"];
mysql_connect("localhost", "root", "");
mysql_select_db("sistema");
mysql_query("UPDATE tabela SET razao = '$razao', fantasia = '$fantasia', cnpj = '$cnpj', logradouro = '$logradouro', numero='$numero', bairro='$bairro', cep='$cep', cidade = '$cidade', uf='$uf', email = '$email', telefone = '$telefone', contato = '$contato', estado = '$estado', obs = '$obs' WHERE tabela.id_cliente = $id");
mysql_close();
header("Location: consulta.php");
?>
You need to add 'selected' to the option that you want to be selected, based on a value from the form/db. Here is an example using $value as the option value that you want selected.
<select class="form-control" name="estado" id="estado">
<option <?php echo $value == 'Ativo' ? selected : '' ?>>Ativo</option>
<option <?php echo $value == 'Inativo' ? selected : '' ?>>Inativo</option>
</select>
Also, your <select> tag does not require a 'value' element..
The HTML select element does not have a value attribute. For the select to do what you want you need to add the selected attribute to the option you want selected. It's always showing as 'Active' because that's the first option and it is the default.
The resulting post-php HTML will need to look something like this stripped back example for 'Inactive' to be selected.
<select>
<option>Active</option>
<option selected>Inactive</option>
</select>
Thank's for all the help!
The solution I've found was:
<?php
$resultado = mysql_query("select * from tabela where id_cliente = $id");
$dados = mysql_fetch_array($resultado);
$query = mysql_query("SELECT * FROM estado");
?>
And the html part:
<select class="form-control" name="estado" id="estado">
<option selected="selected"><?php echo $dados["estado"];?></option>
<option value="Ativo">Ativo</option>
<option value="Inativo">Inativo</option>
</select>

post multiple fields with same name

I have a form by multiple field. In this form i can add multiple group field by prese a button via javascript. To understand exactly what I mean please see fiddle:
https://jsfiddle.net/alihesari/060ym890/2/
How to get all added fields by php and insert theme in mysql after submit?
<fomr action="">
<button type="button" id="add_hotel" class="btn btn-primary">
Add Hotel</button>
<button type="button" id="remove_hotel" class="btn btn-warning">Remove Hotel</button>
<ul class="hotels_ul">
<li class="hotel_li">
<div class="row">
<div class="col-md-10">
<div class="form-group">
<label for="hotel_name[]">Hotel Name:</label>
<input type="text" name="hotel_name[]" id="hotel_name[]" class="form-control" placeholder="Hotel Name">
</div>
</div>
<div class="col-md-10">
<div class="form-group">
<label for="hotelRate[]">Hotel Rate: </label>
<select name="hotelRate[]" id="hotelRate[]" class="form-control">
<option value=""></option>
<option value="متل">متل</option>
<option value="یک ستاره">یک ستاره</option>
<option value="دو ستاره">دو ستاره</option>
<option value="سه ستاره">سه ستاره</option>
<option value="سه ستاره تاپ">سه ستاره تاپ</option>
<option value="چهار ستاره">چهار ستاره</option>
<option value="چهار ستاره تاپ">چهار ستاره تاپ</option>
<option value="پنج ستاره">پنج ستاره</option>
<option value="پنج ستاره تاپ">پنج ستاره تاپ</option>
<option value="هفت ستاره">هفت ستاره</option>
<option value="هتل آپارتمان">هتل آپارتمان</option>
</select>
</div>
</div>
<div class="col-md-10">
<div class="form-group">
<label for="room1[]">Room 1</label>
<input name="room1[]" id="room1[]" value="" class="form-control" placeholder="Price">
</div>
</div>
<div class="col-md-10">
<div class="form-group">
<label for="room2[]">Room 2</label>
<input name="room2[]" id="room2[]" value="" class="form-control" placeholder="Price">
</div>
</div>
<div class="col-md-10">
<div class="form-group">
<label for="room3[]">Room 3</label>
<input name="room3[]" id="room3[]" value="" class="form-control" placeholder="Price">
</div>
</div>
<div class="col-md-10">
<div class="form-group">
<label for="room4[]">Room 4</label>
<input name="room4[]" id="room4[]" value="" class="form-control" placeholder="Price">
</div>
</div>
<div class="col-md-20">
<div class="form-group">
<label for="desc[]">Description</label>
<textarea class="form-control" name="desc[]" id="desc[]" placeholder="Description"></textarea>
</div>
</div>
</div>
</li>
</ul>
<input type="submit" value="submit">
</form>
Here's an example of how you can handle the forms with both JS and PHP.
(if the JS example here doesn't work, see https://jsfiddle.net/n6kzxj4m/)
$(function() {
$(".add").on('click', function(e) {
$($(".hotel").last().clone(true, true)).insertAfter($(".hotel").last());
// reset new items
$(".hotel").last().find(':input:not(select)').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
$(".hotel").last().find('[select]').prop('selectedIndex', 0);
$(".hotel").last().find(':checkbox, :radio').prop('checked', false);
});
$(".remove").on('click', function(e) {
$(this).parents(".hotel").remove();
});
$("form").on('submit', function(e) {
e.preventDefault();
var $form = $(this);
$("#output").html($form.serializeJSON());
});
});
// plugin below only for debug on jsfiddle
/**
* jQuery serializeObject
* #copyright 2014, macek <paulmacek#gmail.com>
* #link https://github.com/macek/jquery-serialize-object
* #license BSD
* #version 2.4.5
*/
!function(e,r){if("function"==typeof define&&define.amd)define(["exports","jquery"],function(e,i){return r(e,i)});else if("undefined"!=typeof exports){var i=require("jquery");r(exports,i)}else r(e,e.jQuery||e.Zepto||e.ender||e.$)}(this,function(e,r){function i(e,i){function n(e,r,i){return e[r]=i,e}function a(e,r){for(var i,a=e.match(t.key);void 0!==(i=a.pop());)if(t.push.test(i)){var o=s(e.replace(/\[\]$/,""));r=n([],o,r)}else t.fixed.test(i)?r=n([],i,r):t.named.test(i)&&(r=n({},i,r));return r}function s(e){return void 0===h[e]&&(h[e]=0),h[e]++}function o(e){switch(r('[name="'+e.name+'"]',i).attr("type")){case"checkbox":return"on"===e.value?!0:e.value;default:return e.value}}function u(r){if(!t.validate.test(r.name))return this;var i=a(r.name,o(r));return c=e.extend(!0,c,i),this}function f(r){if(!e.isArray(r))throw new Error("formSerializer.addPairs expects an Array");for(var i=0,t=r.length;t>i;i++)this.addPair(r[i]);return this}function d(){return c}function l(){return JSON.stringify(d())}var c={},h={};this.addPair=u,this.addPairs=f,this.serialize=d,this.serializeJSON=l}var t={validate:/^[a-z_][a-z0-9_]*(?:\[(?:\d*|[a-z0-9_]+)\])*$/i,key:/[a-z0-9_]+|(?=\[\])/gi,push:/^$/,fixed:/^\d+$/,named:/^[a-z0-9_]+$/i};return i.patterns=t,i.serializeObject=function(){return this.length>1?new Error("jquery-serialize-object can only serialize one form at a time"):new i(r,this).addPairs(this.serializeArray()).serialize()},i.serializeJSON=function(){return this.length>1?new Error("jquery-serialize-object can only serialize one form at a time"):new i(r,this).addPairs(this.serializeArray()).serializeJSON()},"undefined"!=typeof r.fn&&(r.fn.serializeObject=i.serializeObject,r.fn.serializeJSON=i.serializeJSON),e.FormSerializer=i,i});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="#">
<fieldset class="hotel">
<legend>Add a hotel</legend>
<p><label>Hotel: <input type="text" name="hotel[name][]" /></label></p>
<p><label>Rate: <select name="hotel[rate][]"><option>123</option><option>456</option></select></label></p>
<p><input type="button" class="add" value="Add hotel" /> <input type="button" class="remove" value="Remove this hotel" /></p>
</fieldset>
<p><input type="submit" value="Search!" /></p>
</form>
<pre id="output"></pre>
Then, for the PHP handling, you can do:
<?php
if(!empty($_POST)) {
// handle items. $key is the index.
foreach($_POST['hotel']['name'] as $key => $hotel) {
$name = $hotel;
$rate = $_POST['hotel']['rate'][$key];
echo "Name: $name<br>Rate: $rate<hr>";
}
}
NOTE: You may want to check to ensure the number of items in $_POST['hotel']['name'] match the number of items in $_POST['hotel']['rate']. This method may not be ideal, and you may want to assign an ID per hotel row item via JS. But this is a basic approach to your question.

Categories