make text input hide and show with radio button - php

I am trying to make a radio button with 2 options i.e. option A and Option B.
I have 2 text input ( A and B ) and some other text input that always has to be there.
Option A of the radio button has to make disappear text input (A) and show another text input (B). Option B should make this the opposite. ( disappear text input (B) and show another text input (A) )
I don't know how to make that 2 text inputs.
will appreciate some help.
<form class="user" id="RegisterForm" action="" method="post">
<? print("USER ROLE : ") ?>
<input type="radio" name="user_role" <?php if (isset($user_role) && $user_role=="A") echo "checked";?> value="A">A
<input type="radio" name="user_role" <?php if (isset($user_role) && $user_role=="B") echo "checked";?> value="B">B
//REST OF the code defines some text inputs that are always should show.
<?php if ($fullname_req != "-1") { ?>
<div class="form-group">
<input type="text" class="form-control form-control-user" id="displayname" name="fullname" placeholder="<?php _e('Display name', 'user-panel-pro') ?>">
</div>
<?php } ?>
<?php if ($firstname_req != "-1") { ?>
<div class="form-group row">
<?php if ($firstname_req != "-1" or $lastname_req != "-1") { ?>
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="text" class="form-control form-control-user" id="First-Name" name="firstname" placeholder="<?php _e('first name', 'user-panel-pro') ?>">
</div>
<?php } ?>
<?php if ($lastname_req != "-1") { ?>
<div class="col-sm-6">
<input type="text" class="form-control form-control-user" id="Last-Name" name="lastname" placeholder="<?php _e('last name', 'user-panel-pro') ?>">
</div>
<?php } ?>
</div>
<?php } ?>
<?php if ($username_req != "-1") { ?>
<div class="form-group">
<input type="text" class="form-control form-control-user" id="User-Name" name="username" placeholder="<?php _e('Username', 'user-panel-pro') ?>">
</div>
<?php } ?>
<?php if ($email_req != "-1") { ?>
<div class="form-group">
<input type="email" class="form-control form-control-user" id="Email" name="email" placeholder="<?php _e('Email', 'user-panel-pro') ?>">
</div>
<?php } ?>

I am not able to understand your code, so I am just giving a small demo
<div class="radioBtns">
<label for='rbtnLoginTypeMobile'><input checked type="radio" id="rbtnLoginTypeMobile" name="rbtnLoginType" value='1' onclick="showLogin(1)"/><span>Mobile</span></label>
<label for='rbtnLoginTypeEmail'><input type="radio" id="rbtnLoginTypeEmail" name="rbtnLoginType" value='0' onclick="showLogin(0)" /><span>Email</span></label>
</div>
<div class="mobilelogin">Your Mobile Div</div>
<div class="emaillogin">Your Email Div</div>
<script type="text/javascript">
function showLogin(val)
{
if(val==1)
{
$('.emaillogin').css('display','none');
$('.mobilelogin').css('display','');
}
else
{
$('.mobilelogin').css('display','none');
$('.emaillogin').css('display','');
}
}
</script>

Related

Change data requirements of an input field based on the value of a select field in a web form

I am working on a web form that includes a select drop down with two options: "Cedula" (in English, "Identification") and "Pasaporte" (in English, "Passport").
Here is an image of my web form so far.
Please help me achieve the following goal: when the user selects "Cedula", they are limited to 10 digits, but when they select "Pasaporte, they are not limited to 10 digits.
Here is my code so far:
<?php
if ($_GET['id']) {
$cliente = $clienteNegocio->recuperar($_GET['id']);
$txtAction = 'Editar';
}else{
$cliente = new cliente();
$txtAction = 'Agregar';
}
?>
<div class="container">
<div class="page-header">
<h1><?php echo $txtAction; ?> Cliente</h1>
</div>
<form role="form" method="post" id="principal">
<input type="hidden" name="id" value="<?php echo $cliente->getId();?>" >
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control" id="nombre" name="nombre" placeholder="Nombre" value="<?php echo $cliente->getNombre();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="apellido">Apellidos</label>
<input type="text" class="form-control" id="apellido" name="apellido" placeholder="Apellido" value="<?php echo $cliente->getApellido();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="tipoDoc">Tipo de Documento</label>
<select class="form-control" id="tipoDoc" name="tipoDoc">
<option value="Cedula" <?php if($cliente->getTipoDoc() == 'Cedula') {echo "selected";} ?> >Cedula</option>
<option value="Pasaporte" <?php if($cliente->getTipoDoc() == 'Pasaporte') {echo "selected";} ?> >Pasaporte</option>
</select>
</div>
<div class="form-group">
<label for="nroDoc">Numero de Documento</label>
<input type="number" class="form-control" id="nroDoc" maxlength=10 oninput="if(this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"
name="nroDoc" placeholder="Numero de Documento" value="<?php echo $cliente ->getNroDoc();?>" required>
<div class="help-block with-errors"></div>
</div>
There you go.
I have written a javascript function to check the length with you select Pasaporte/Cedula.
Secondly, in <input type = "number"/> you cannot set maxLength. Hence you have to set <input type = "text" />. Also, an onKeyPress event to verify the input as number
<?php
if ($_GET['id']) {
$cliente = $clienteNegocio->recuperar($_GET['id']);
$txtAction = 'Editar';
}else{
$cliente = new cliente();
$txtAction = 'Agregar';
}
?>
<script>
function setMaxLength(){
var inputVal = document.getElementById("tipoDoc")
var selIndex = inputVal.options[inputVal.selectedIndex].value
var inputNum = document.getElementById("nroDoc");
if( selIndex === "Cedula"){
inputNum.maxLength = 10
selIndex.substr(0, 9);
inputNum.value = inputNum.value.substr(0, 9);
} else{
// Set your own limit here
// if selIndex === "Pasaporte"
inputNum.maxLength = 20
}
}
</script>
<div class="container">
<div class="page-header">
<h1><?php echo $txtAction; ?> Cliente</h1>
</div>
<form role="form" method="post" id="principal">
<input type="hidden" name="id" value="<?php echo $cliente->getId();?>" >
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control" id="nombre" name="nombre" placeholder="Nombre" value="<?php echo $cliente->getNombre();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="apellido">Apellidos</label>
<input type="text" class="form-control" id="apellido" name="apellido" placeholder="Apellido" value="<?php echo $cliente->getApellido();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="tipoDoc">Tipo de Documento</label>
<select class="form-control" id="tipoDoc" name="tipoDoc" onChange="setMaxLength()">
<option value="Cedula" <?php if($cliente->getTipoDoc() == 'Cedula') {echo "selected";} ?> >Cedula</option>
<option value="Pasaporte" <?php if($cliente->getTipoDoc() == 'Pasaporte') {echo "selected";} ?> >Pasaporte</option>
</select>
</div>
<div class="form-group">
<label for="nroDoc">Numero de Documento</label>
<!--
Here onKeyPress method is used to check if the input is a number
in <input type = "number"/> you cannot set maxLength
hence you have to set <input type = "text" />
-->
<input type="text" class="form-control" id="nroDoc" maxlength=10 onkeypress="if ( isNaN(this.value + String.fromCharCode(event.keyCode) )) return false;"
name="nroDoc" placeholder="Numero de Documento" value="<?php echo $cliente ->getNroDoc();?>" required>
<div class="help-block with-errors"></div>
</div>
</form>
</div>

How to retain the form inputs when submit button is clicked?

So I got a form that gets the student basic information for temporary admission, so I have a function that when register/submit button is clicked it checks if the Or number is already exist in database when it does it displays error message and the inputted data stays in their own fields.
the function works but when a user inputted a name for example "james joseph" and or number exist in database it will retain the name "james" only.
is there a way that it retains both?
Here's my code for the form
<form class="form-group" method="post" action="index.php">
<div class="row"><!--Top row-->
<div class="col-lg-5"><p><h3>Register an Enrollee</h3></p></div>
<div class="col-lg-3"></div>
<div class="col-lg-4">
</div>
</div>
<br>
<br>
<div class="row">
<div class="col-lg-4">
<label>Academic Year </label>
<?php
if (isset($_GET['academic'])) {
$academic=$_GET['academic'];
echo '<input type="text" name="academic" class="form-control" required value='.$academic.'>';
# code...
}
else
{
echo '<input type="text" name="academic" class="form-control" required>';
}
?>
</div>
<div class="col-lg-4">
<label for="year_level">Year Level</label>
<?php
if (isset($_GET['year'])) {
$year=$_GET['year'];
echo '<input type="text" name="year" class="form-control" required value='.$year.'>';
# code...
}
else
{
echo '<input type="text" name="year" class="form-control" required>';
}
?>
</div>
<div class="col-lg-4">
<label>OR Number</label>
<?php
if (isset($_GET['or'])) {
$or=$_GET['or'];
echo '<input type="text" name="or" class="form-control" required value='.$or.'>';
# code...
}
else
{
echo '<input type="text" name="or" class="form-control" required>';
}
?>
</div>
</div>
<br>
<div class="row">
<div class="col-lg-4">
<label>First Name</label>
<?php
if (isset($_GET['firstname'])) {
$firstname=$_GET['firstname'];
echo '<input type="text" name="firstname" class="form-control" required value='.$firstname.'>';
# code...
}
else
{
echo '<input type="firstname" name="firstname" class="form-control" required>';
}
?>
</div>
<div class="col-lg-4">
<label>Last Name</label>
<?php
if (isset($_GET['lastname'])) {
$lastname=$_GET['lastname'];
echo '<input type="text" name="lastname" class="form-control" required value='.$lastname.'>';
# code...
}
else
{
echo '<input type="text" name="lastname" class="form-control" required>';
}
?>
</div>
<div class="col-lg-4">
<label>Middle Initial</label>
<?php
if (isset($_GET['middle'])) {
$middle=$_GET['middle'];
echo '<input type="text" name="middle" class="form-control" required value='.$middle.'>';
# code...
}
else
{
echo '<input type="text" name="middle" class="form-control" required>';
}
?>
</div>
</div><!--End of Second row-->
<br>
<button class="btn btn-primary btn-block" type="submit" name="register">Register</button>
</form>
Here is the php code that checks and gets the inputted data from the form
if (isset($_POST['register'])) {
# code...
$year_level=$_POST['year'];
$academic_year=$_POST['academic'];
$or_number=$_POST['or'];
$stud_fname=$_POST['firstname'];
$stud_lname=$_POST['lastname'];
$stud_mi=$_POST['middle'];
$or_check_query="SELECT * FROM tbl_user WHERE or_number=$or_number";
$result=mysqli_query($conn,$or_check_query);
$or=mysqli_fetch_assoc($result);
if($or['or_number']===$or_number) {
header("Location: index.php?signup=failed&academic=$academic_year&year=$year_level&or=$or_number&firstname=$stud_fname&lastname=$stud_lname&middle=$stud_mi");
# code...
}
else {
$sql= "INSERT INTO tbl_user(or_number,academic,year,firstname,lastname,middle)
VALUES ('$or_number','$academic_year','$year_level','$stud_fname','$stud_lname','$stud_mi')";
mysqli_query($conn,$sql);
header("Location: index.php?signup=success");
}
}

How to update 3 table existing data by id in codeigniter

I have data that already exists in the database server and I want to change it based on the id table. I want to update one time so that the 3 data tables are updated as well because the table id is in another table too. I tried it but I can't. Please help me
I have joined from several tables.
I've also searched on :link!
:link!
This is my Controller
public function updateservicenew(){
$id= $this->uri->segment(3);
$data['vendor']= $this->model_vendor->show_data_all();
$data['vendor2']= $this->model_vendor->show_data_vendor2();
$data['service']= $this->Madmin2->getservicenew($id)->row_array();
$this->template->viewadmin2('admin2/transaks/updateservicenew',$data);
}
public function saveservicenew(){
$this->Madmin2->saveservicenew($this->input->post());
redirect(base_url('admin2/transaksidone'));
}
This is my Model
public function getservicenew($id){ $this->db->select('kontak_sis.*,vendor.*,vendor2.*,service_sis.*,sumber_info.*,provinsi.*,kota.*,mst_service.*,service.*');
$this->db->from('service_sis');
$this->db->join('vendor', 'service_sis.id_vendor=vendor.id_vendor', 'left');
$this->db->join('vendor2', 'service_sis.id_vendor2=vendor2.id_vendor2', 'left');
$this->db->join('mst_service', 'service_sis.id_mst_service=mst_service.id_mst_service', 'left');
$this->db->join('kontak_sis', 'service_sis.id_kontak=kontak_sis.id_kontak', 'left');
$this->db->join('sumber_info', 'service_sis.id_sumber=sumber_info.id_sumber', 'left');
$this->db->join('provinsi', 'service_sis.id_provinsi=provinsi.id_provinsi', 'left');
$this->db->join('kota', 'service_sis.id_kota=kota.id_kota', 'left');
$this->db->join('service', 'service_sis.id_service=service.id_service', 'left');
$param = array('service_sis.id_service'=>$id);
return $this->db->get_where('',$param);
}
public function simpanservicenew(){
$data_pelanggan['cid']= $this->input->post('cid');
$data_pelanggan['nama_perusahaan']= $this->input->post('nama_perusahaan');
$data_pelanggan['destination']= $this->input->post('destination');
$data_pelanggan['alamat_perusahaan']= $this->input->post('alamat_perusahaan');
$data_pelanggan['pic_perusahaan']= $this->input->post('pic_perusahaan');
$data_pelanggan['telepon_pic']= $this->input->post('telepon_pic');
$data_pelanggan['fax']= $this->input->post('fax');
$data_pelanggan['hp']= $this->input->post('hp');
$data_pelanggan['email_pic']= $this->input->post('email_pic');
$this->db->where('id_kontak',$this->input->post('id_kontak'));
$this->db->update('kontak_sis',$data_pelanggan);
$data_vendor['pic_vendor']= $this->input->post('pic_vendor');
$data_vendor['media_vendor']= $this->input->post('media_vendor');
$data_vendor['kapasitas_vendor']= $this->input->post('kapasitas_vendor');
$data_vendor['telepon_vendor']= $this->input->post('telepon_vendor');
$data_vendor['email_vendor']= $this->input->post('email_vendor');
$this->db->where('id_vendor',$this->input->post('id_vendor'));
$this->db->update('vendor',$data_vendor);
$data_vendor2['pic_vendor2']= $this->input->post('pic_vendor2');
$data_vendor2['media_vendor2']= $this->input->post('media_vendor2');
$data_vendor2['kapasitas_vendor2']= $this->input->post('kapasitas_vendor2');
$data_vendor2['telepon_vendor2']= $this->input->post('telepon_vendor2');
$data_vendor2['email_vendor2']= $this->input->post('email_vendor2');
$this->db->where('id_vendor2',$this->input->post('id_vendor2'));
$this->db->update('vendor2',$data_vendor2);
This is my View
<div class="row">
<div class="box box-primary">
<div class="box-header with-border">
<div id='progress'><div id='progress-complete'></div></div>
<h2 class="box-title"><b>Set Ewo</b></h2>
</div>
<div class="box-body">
<form method="post" action="<?php echo base_url('admin2/simpanservicebaru/'.$this->uri->segment(3)); ?>" enctype="multipart/form-data" id="myForm">
<input type="hidden" name="id_service" value="<?php echo $service['id_service']; ?>">
<input type="hidden" name="id_kontak" value="<?php echo $service['id_kontak']; ?>">
<input type="hidden" name="id_mst_service" value="<?php echo $service['id_mst_service']; ?>">
<input type="hidden" name="id_vendor" value="<?php echo $service['id_vendor']; ?>">
<input type="hidden" name="id_vendor2" value="<?php echo $service['id_vendor2']; ?>">
<fieldset>
<div class="form-group">
<label for="">Registration Form ID</label>
<input type="text" name="cid" class="form-control" placeholder="" value="<?php echo $service['cid'] ?>" readonly>
</div>
<div class="form-group">
<label for="">Customer</label>
<b><input type="text" name="nama_perusahaan" class="form-control" value="<?php echo $service['nama_perusahaan']; ?>"required></b>
</div>
<div class="form-group">
<label for="">Destination</label>
<b><input type="text" name="destination" class="form-control" placeholder="destination"
value="<?php echo $service['destination']; ?>"required></b>
</div>
<div class="form-group">
<label for="">Address</label>
<b><input type="text" name="alamat_perusahaan" class="form-control" placeholder="alamat"
value="<?php echo $service['alamat_perusahaan']; ?>"required></b>
</div>
<div class="form-group">
<label for="">PIC(Engineering)</label>
<input type="text" name="pic_perusahaan" class="form-control" placeholder="pic"
value="<?php echo $service['pic_perusahaan']; ?>">
</div>
<div class="form-group">
<label for="">Phone Number</label>
<input type="text" name="telepon_pic" class="form-control" placeholder="phone number"
value="<?php echo $service['telepon_pic']; ?>">
</div>
<div class="form-group">
<label for="">Fax</label>
<input type="text" name="fax" class="form-control" placeholder="fax"
value="<?php echo $service['fax']; ?>">
</div>
<div class="form-group">
<label for="">HP</label>
<input type="text" name="hp" class="form-control" placeholder="hp"
value="<?php echo $service['hp']; ?>">
</div>
<div class="form-group">
<label for="">Email</label>
<input type="text" name="email_pic" class="form-control" placeholder="email pic"
value="<?php echo $service['email_pic']; ?>">
</div>
</fieldset>
<fieldset>
<div class="form-group">
<label for="">Vendor1</label>
<select name="id_vendor" class="form-control">
<option value="0">Non Vendor</option>
<?php
foreach ($vendor as $c)
{ ?>
<?php $sel = ($c->id_vendor==$service['id_vendor']) ? 'selected' :''; ?>
<option value="<?php echo $c->id_vendor;?>" <?php echo $sel;?>><?php echo $c->nama_vendor;?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="">PIC</label>
<input type="text" name="pic_vendor" class="form-control" placeholder="pic vendor"
value="<?php echo $service['pic_vendor']; ?>">
</div>
<div class="form-group">
<label for="">Media</label>
<input type="text" name="media_vendor" class="form-control" placeholder="media"
value="<?php echo $service['media_vendor']; ?>">
</div>
<div class="form-group">
<label for="">Capacity</label>
<input type="text" name="kapasitas_vendor" class="form-control" placeholder="kapasitas vendor"
value="<?php echo $service['kapasitas_vendor']; ?>">
</div>
<div class="form-group">
<label for="">Phone</label>
<input type="text" name="telepon_vendor" class="form-control" placeholder="telepon vendor"
value="<?php echo $service['telepon_vendor']; ?>">
</div>
<div class="form-group">
<label for="">Email</label>
<input type="text" name="email_vendor" class="form-control" placeholder="email vendor"
value="<?php echo $service['email_vendor']; ?>">
</div>
<hr></hr>
<div class="form-group">
<label for="">Vendor2</label>
<select name="id_vendor2" class="form-control">
<option value="0">Non Vendor</option>
<?php
foreach ($vendor2 as $c)
{ ?>
<?php $sel = ($c->id_vendor2==$service['id_vendor2']) ? 'selected' :''; ?>
<option value="<?php echo $c->id_vendor2;?>" <?php echo $sel;?>><?php echo $c->nama_vendor2;?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="">PIC</label>
<input type="text" name="pic_vendor2" class="form-control" placeholder="pic vendor"
value="<?php echo $service['pic_vendor2']; ?>">
</div>
<div class="form-group">
<label for="">Media</label>
<input type="text" name="media_vendor2" class="form-control" placeholder="media"
value="<?php echo $service['media_vendor2']; ?>">
</div>
<div class="form-group">
<label for="">Capacity</label>
<input type="text" name="kapasitas_vendor2" class="form-control" placeholder="kapasitas vendor"
value="<?php echo $service['kapasitas_vendor2']; ?>">
</div>
<div class="form-group">
<label for="">Phone</label>
<input type="text" name="telepon_vendor2" class="form-control" placeholder="telepon vendor"
value="<?php echo $service['telepon_vendor2']; ?>">
</div>
<div class="form-group">
<label for="">Email</label>
<input type="text" name="email_vendor2" class="form-control" placeholder="email vendor"
value="<?php echo $service['email_vendor2']; ?>">
</div>
</fieldset>
<fieldset>
<br>
<p>*Pastikan semua form sudah terisi dengan benar</p>
<button id="submit" class="btn btn-success">Update Service</button>
</fieldset>
</div>
</div>
</div>
</div>
<script>
$( function() {
var $signupForm = $( '#myForm' );
$signupForm.validate({errorElement: 'em'});
$signupForm.formToWizard({
submitButton: 'submit',
nextBtnName: 'Selanjutnya',
prevBtnName: 'Sebelumnya',
nextBtnClass: 'btn btn-primary btn-flat next',
prevBtnClass: 'btn btn-default btn-flat prev',
buttonTag: 'button',
validateBeforeNext: function(form, step) {
var stepIsValid = true;
var validator = form.validate();
$(':input', step).each( function(index) {
var xy = validator.element(this);
stepIsValid = stepIsValid && (typeof xy == 'undefined' || xy);
});
return stepIsValid;
},
progress: function (i, count) {
$('#progress-complete').width(''+(i/count*100)+'%');
}
});
});
</script>
Tables:
This is my **Table contact**
# Name type
1 id_kontak int(11)
2 cid varchar(10)
3 nama_perusahaan varchar(150)
4 destination varchar(30)
5 alamat_perusahaan varchar(200)
6 pic_perusahaan varchar(200)
7 telepon_pic varchar(30)
8 fax int(15)
9 hp int(15)
10 email_pic (15)
This my **table vendor**
# Name type
1 id_vendor int(11)
2 pic_vendor varchar(200)
3 telepon_vendor varchar(100)
4 media_vendor varchar(200)
5 kapasitas_vendor varchar(200)
6 email_vendor varchar(200)
This my **table vendor2**
# Name type
1 id_vendor2 int(11)
2 pic_vendor2 varchar(200)
3 telepon_vendor2 varchar(100)
4 media_vendor2 varchar(200)
5 kapasitas_vendor2 varchar(200)
6 email_vendor2 varchar(200)
I want to update one time so that the 3 data tables are updated as well because the table id is in another table too.Thank you for the help
What i understand from your code is that you want to update 3 tables using single query. In that case you can use following code.
$this->db->set('a.cid', $this->input->post('cid'));
$this->db->set('a.nama_perusahaan', $this->input->post('nama_perusahaan'));
$this->db->set('b.pic_vendor', $this->input->post('pic_vendor'));
$this->db->set('b.media_vendor', $this->input->post('media_vendor'));
$this->db->set('c.pic_vendor2', $this->input->post('pic_vendor2'));
$this->db->set('c.companyaddress', $this->input->post('pic_vendor2'));
$this->db->where('a.id', 1);
$this->db->where('a.id = b.id');
$this->db->where('a.id = c.id');
$this->db->update('table as a, table2 as b',table3 as c);
But in na More Professional way you should use Codeigniter Transactions. If any of the query fails the other one gets rolled back.
$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete();

CodeIgniter Form validation fails and doesn't show a message

I have a dynamic form that is generated depending on how many people are selected from another page. When I submit this form however, the form validation fails without giving me an error. Could someone take a look and see why it's failing? I have tried debugging it but I can't see it. Maybe my method is wrong? This form used to work as well. It started to not work after I added the form processing for the children. Hopefully someone can help. Thank you so much.
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Details extends TX_Controller {
public function __construct(){
parent::__construct();
ini_set('memory_limit', '64M');
$this->load->model('TransactionsModel');
$this->load->model('ProductsModel');
}
public function index(){
$data['productdetail'] = $this->ProductsModel->getProduct($this->session->userdata('productid'));
// var_dump($data['productdetail']);
$data['adults'] = $this->session->userdata('adults');
$data['children'] = $this->session->userdata('children');
$this->load->view('public/publicMenu/navigationLink');
$this->load->view('public/publicDetails/details',$data);
$this->load->view('public/publicMenu/navigationJquery');
}
public function next(){
$adultlength = $this->input->post('adults');
$childrenlength = $this->input->post('children');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
for ($i=0; $i < $adultlength; $i++) {
$this->form_validation->set_rules('inputfirstname['.$i.']', 'Firstname', 'trim|required');
$this->form_validation->set_rules('inputlastname['.$i.']', 'Lastname', 'trim|required');
$this->form_validation->set_rules('inputdateofbirth['.$i.']', 'Date of Birth', 'trim|required');
$this->form_validation->set_rules('inputicnumber['.$i.']', 'IC Number', 'trim|required');
$this->form_validation->set_rules('inputmobilenumber['.$i.']', 'Mobile Number', 'trim|required');
$this->form_validation->set_rules('inputemail['.$i.']', 'Email', 'trim|required');
$this->form_validation->set_rules('inputconfirmemail['.$i.']', 'Confirm Email', 'trim|required');
$inputfirstname[] = $this->input->post('inputfirstname['.$i.']');
$inputlastname[] = $this->input->post('inputlastname['.$i.']');
$inputdateofbirth[] = $this->input->post('inputdateofbirth['.$i.']');
$inputicnumber[] = $this->input->post('inputicnumber['.$i.']');
$inputmobilenumber[] = $this->input->post('inputmobilenumber['.$i.']');
$inputemail[] = $this->input->post('inputemail['.$i.']');
$inputpostcode[] = $this->input->post('inputpostcode['.$i.']');
}
for($j=0;$j<$childrenlength;$j++){
$inputchildfirstname[] = $this->input->post('inputchildfirstname['.$j.']');
$inputchildlastname[] = $this->input->post('inputchildlastname['.$j.']');
$inputchilddateofbirth[] = $this->input->post('inputchilddateofbirth['.$j.']');
}
if($this->form_validation->run()==false){
$data['productdetail'] = $this->ProductsModel->getProduct($this->session->userdata('productid'));
$data['adults'] = $this->session->userdata('adults');
$data['children'] = $this->session->userdata('children');
$this->load->view('public/publicMenu/navigationLink');
$this->load->view('public/publicDetails/details',$data);
$this->load->view('public/publicMenu/navigationJquery');
}else{
for ($i=0; $i < $adultlength; $i++) {
$passengerdetails[] = array(
'firstname'=>$inputfirstname[$i],
'lastname'=>$inputlastname[$i],
'dateofbirth'=>$inputdateofbirth[$i],
'icnumber'=>$inputicnumber[$i],
'mobilenumber'=>$inputmobilenumber[$i],
'email'=>$inputemail[$i],
'postcode'=>$inputpostcode[$i],
'usertype'=>'adult'
);
}
if($childrenlength>0){
for($j=0;$j<$childrenlength;$j++){
$childpassengerdetails[] = array(
'firstname'=>$inputchildfirstname[$j],
'lastname'=>$inputchildlastname[$j],
'dateofbirth'=>$inputchilddateofbirth[$j],
'icnumber'=>'',
'mobilenumber'=>'',
'email'=>'',
'postcode'=>'',
'usertype'=>'child'
);
}
$this->session->set_userdata('childpassengerdetails',json_encode($childpassengerdetails));
}
$this->session->set_userdata('passengerdetails',json_encode($passengerdetails));
redirect('/Public/Payment');
}
}
}
View
<section id="about" class="container content-section text-center">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<h2><?php echo $productdetail->name;?></h2>
<h2>Enter Passenger Details</h2>
<?php echo form_open_multipart('Public/Details/next','class="inputform"');?>
<h3>Adults</h3>
<?php for($i=0;$i<$adults;$i++){?>
<input type="hidden" class="form-control" name="adult" value="<?php echo $adults;?>">
<label for="inputfirstname">Firstname</label>
<input type="text" class="form-control" name="inputfirstname[]" placeholder="Firstname" value="<?php echo set_value('inputfirstname['.$i.'],""');?>">
<div class="errormessage"><?php echo form_error('inputfirstname['.$i.']'); ?></div>
<label for="inputfirstname">Lastname</label>
<input type="text" class="form-control" name="inputlastname[]" placeholder="Lastname" value="<?php echo set_value('inputlastname[$i]');?>">
<div class="errormessage"><?php echo form_error('inputlastname['.$i.']'); ?></div>
<label for="inputdateofbirth">Date of Birth</label>
<input type="date" class="form-control" name="inputdateofbirth[]" value="<?php echo set_value('inputdateofbirth[$i]');?>">
<div class="errormessage"><?php echo form_error('inputdateofbirth['.$i.']'); ?></div>
<label for="inputicnumber">IC Number</label>
<input type="text" class="form-control" name="inputicnumber[]" placeholder="IC Number" value="<?php echo set_value('inputicnumber[$i]');?>">
<div class="errormessage"><?php echo form_error('inputicnumber['.$i.']'); ?></div>
<label for="inputmobilenumber">Mobile Number</label>
<input type="text" class="form-control" name="inputmobilenumber[]" placeholder="Mobile Number" value="<?php echo set_value('inputmobilenumber[$i]');?>">
<div class="errormessage"><?php echo form_error('inputmobilenumber['.$i.']'); ?></div>
<label for="inputemail">Email</label>
<input type="text" class="form-control" name="inputemail[]" placeholder="Email" value="<?php echo set_value('inputemail[$i]');?>">
<div class="errormessage"><?php echo form_error('inputemail['.$i.']'); ?></div>
<label for="inputconfirmemail">Confirm Email</label>
<input type="text" class="form-control" name="inputconfirmemail[]" placeholder="Confirm Email" value="<?php echo set_value('inputconfirmemail[$i]');?>">
<div class="errormessage"><?php echo form_error('inputconfirmemail['.$i.']'); ?></div>
<label for="inputaddress1">Address</label>
<input type="text" class="form-control" name="inputaddress1[]" placeholder="Address 1" value="<?php echo set_value('inputaddress1[$i]');?>">
<input type="text" class="form-control" name="inputaddress2[]" placeholder="Address 2" value="<?php echo set_value('inputaddress2[$i]');?>">
<input type="text" class="form-control" name="inputaddress3[]" placeholder="Address 3" value="<?php echo set_value('inputaddress3[$i]');?>">
<input type="text" class="form-control" name="inputaddress4[]" placeholder="Address 4" value="<?php echo set_value('inputaddress4[$i]');?>">
<input type="text" class="form-control" name="inputaddress5[]" placeholder="Address 5" value="<?php echo set_value('inputaddress5[$i]');?>">
<label for="inputpostcode">Postcode</label>
<input type="text" class="form-control" name="inputpostcode[]" placeholder="Postcode1" value="<?php echo set_value('inputpostcode[$i]');?>">
<div class="errormessage"><?php echo form_error('inputpostcode['.$i.']'); ?></div>
<?php } ?>
<?php if($children>0){ ?>
<h3>Children</h3>
<?php for($j=0;$j<$children;$j++){ ?>
<input type="hidden" class="form-control" name="children" value="<?php echo $children;?>">
<label for="inputchildfirstname">Firstname</label>
<input type="text" class="form-control" name="inputchildfirstname[]" value="<?php echo set_value('inputchildfirstname[$j]');?>" placeholder="Firstname">
<label for="inputchildlastname">Lastname</label>
<input type="text" class="form-control" name="inputchildlastname[]" value="<?php echo set_value('inputchildlastname[$j]');?>" placeholder="Lastname">
<label for="inputchilddateofbirth">Date of Birth</label>
<input type="date" class="form-control" name="inputchilddateofbirth[]" value="<?php echo set_value('inputchilddateofbirth[$j]');?>">
<?php }} ?>
<p><button type="submit" class="btn btn-primary">Next</button></p>
<p>Cancel</p>
<?php echo form_close(); ?>
<p><?php echo $this->session->flashdata('Form'); ?></p>
</div>
</div>
</section>

Form won't submit info to database

I've been building a site recently for a friend and I've gotten stuck on this one form. A button links to url in which this form is on and then once you fill out all the information and click submit, instead of returning you back to home.php it just removes the form from view and all you see is a blank new.php and it doesn't submit the information.
<?php
function renderForm($user, $rank, $position, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<center>
<form action="" method="post">
<div class="form-group">
<label for="username">Username*</label>
<input id="username" class="form-control" type="text" name="user" placeholder="Username" value="<?php echo $user; ?>" />
</div>
<div class="form-group">
<label for="rank">Rank</label>
<select class="form-control" name="rank">
<option value="1">Pending Rank</option>
<option value="2">PVT</option>
</select>
</div>
<div class="form-group">
<label for="position">Position</label>
<input id="position" class="form-control" type="text" name="position" placeholder="MOG/GG" value="<?php echo $position; ?>" />
</div>
<div class="form-group">
<label for="Date">Date*</label>
<input id="Date" class="form-control" type="text" name="date" placeholder="<?php echo date('d M y'); ?>" value="<?php echo $date; ?>" />
</div>
<div class="form-group">
<label for="Tag">Tag*</label>
<input id="Tag" class="form-control" type="text" name="tag" placeholder="[]" value="<?php echo $tag; ?>" />
</div>
<div class="form-group">
<label for="adt">ADT</label>
<input id="adt" class="form-control" type="text" name="adt" placeholder="{TEST}" value="<?php echo $adt; ?>" />
</div>
<div class="form-group">
<label for="exp">EXP</label>
<input id="exp" class="form-control" type="text" name="exp" placeholder="420" value="<?php echo $exp; ?>" />
</div>
<div class="form-group">
<label for="reg">Regiment</label>
<input id="reg" class="form-control" type="text" name="reg" placeholder="[P]" value="<?php echo $reg; ?>" />
</div>
<div class="form-group">
<label for="Notes">Notes</label>
<input id="Notes" class="form-control" type="text" name="notes" placeholder="Notes" value="<?php echo $notes; ?>" />
</div>
<button type="submit" name="submit" class="btn btn-default" value="Submit">Submit</button>
</form>
<script>
$('.modal').on('hidden.bs.modal', function(){
$(this).find('form')[0].reset();
});
</script>
<?php
}
include('config/db.php');
if (isset($_POST['submit']))
{
$user = mysql_real_escape_string(htmlspecialchars($_POST['user']));
$rank = mysql_real_escape_string(htmlspecialchars($_POST['rank']));
$position = mysql_real_escape_string(htmlspecialchars($_POST['position']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$tag = mysql_real_escape_string(htmlspecialchars($_POST['tag']));
$adt = mysql_real_escape_string(htmlspecialchars($_POST['adt']));
$exp = mysql_real_escape_string(htmlspecialchars($_POST['exp']));
$reg = mysql_real_escape_string(htmlspecialchars($_POST['reg']));
$notes = mysql_real_escape_string(htmlspecialchars($_POST['notes']));
$datej = mysql_real_escape_string(htmlspecialchars($_POST['date']));
if ($user == '' || $rank == '' || $date == '' || $tag == '')
{
$error = '<center>ERROR: Please fill in all required fields!</center>';
#renderForm($user, $rank, $position, $error);
}
else
{
mysql_query("INSERT per SET user='$user', rank='$rank', position='$position', date='$date', tag='$tag', adt='$adt', exp='$exp', reg='$reg', notes='$notes', datej='$datej'", $db1)
or die(mysql_error());
include('logsadd.php');
write_mysql_log('has added member <font color="black"><b>'. $user .'</b></font>.', $db);
header("Location: home.php");
}
}
else
header("home.php");
{
#renderForm('','','');
}?>
Your else looks like this
else
header("home.php");
{
#renderForm('','','');
it should be
else
{
// header should be inside the else part
header("Location:home.php");
#renderForm('','','');

Categories