how to print array values in different textbox - php

I have these values inside a table
and I want them to print in each of the textboxes like
Can anyone help me with that?
This is my code so far
PHP
<?php
$spnsrArr = $conn->query("SELECT bap_sponsors.* FROM bap_sponsors WHERE bap_id='$bapID'");
$row = mysqli_fetch_array($spnsrArr);
$spnsrName = $row['sponsors_name'];
$spnsrAge = $row['sponsors_age'];
$spnsrReligion = $row['sponsors_religion'];
$spnsrPlace = $row['sponsors_residence'];
?>
HTML
<label>SPONSOR #1</label>
<div class="form-group">
<div class="col-md-6">
<h5>Name:</h5>
<input type="text" name="$spnsName[]" class="form-control" value="<?php echo $spnsrName; ?>" disabled="" />
</div>
<div class="col-md-2">
<h5>Age:</h5>
<input type="number" name="age[]" class="form-control" value="<?php echo $spnsrAge; ?>" disabled="" />
</div>
<div class="col-md-4">
<h5>Religion:</h5>
<input type="text" name="spnsrreligion[]" class="form-control" value="<?php echo $spnsrReligion; ?>" disabled="" />
</div>
<div class="col-md-12">
<h5>Residence:</h5>
<input type="text" name="spnsrresidence[]" class="form-control" value="<?php echo $spnsrPlace; ?>" disabled="" /><br>
<hr>
</div>
</div>

Your query
$spnsrArr = $conn->query("SELECT bap_sponsors.* FROM bap_sponsors WHERE bap_id='$bapID'");
returns several rows. But
$row = mysqli_fetch_array($spnsrArr);
fetches only one.
You should iterate over results, calling mysqli_fetch_array several times. This is usually done with while:
$i = 0;
while ($row = mysqli_fetch_array($spnsrArr)) {
$spnsrName = $row['sponsors_name'];
$spnsrAge = $row['sponsors_age'];
$spnsrReligion = $row['sponsors_religion'];
$spnsrPlace = $row['sponsors_residence'];
$i++;
?>
<label>SPONSOR #<?php echo $i;?></label>
<div class="form-group">
<div class="col-md-6">
<h5>Name:</h5>
<input type="text" name="spnsName[]" class="form-control" value="<?php echo $spnsrName; ?>" disabled="" />
</div>
<div class="col-md-2">
<h5>Age:</h5>
<input type="number" name="age[]" class="form-control" value="<?php echo $spnsrAge; ?>" disabled="" />
</div>
<div class="col-md-4">
<h5>Religion:</h5>
<input type="text" name="spnsrreligion[]" class="form-control" value="<?php echo $spnsrReligion; ?>" disabled="" />
</div>
<div class="col-md-12">
<h5>Residence:</h5>
<input type="text" name="spnsrresidence[]" class="form-control" value="<?php echo $spnsrPlace; ?>" disabled="" /><br>
<hr>
</div>
</div>
<?php
}
Also note I removed $ in name="$spnsName[]".

Related

Getting error on updating multiple rows using loop in codeigniter

When i am try to update details using for loop it is updated as a empty(Null) to refer columns.
Below is my code please help me what wrong with my logic.
View Code
The No. of text fields are going to generate depends on no. of values present in table. It is executing in foreach loop
<form method="post" action="<?php echo site_url('Home/advisor_update'); ?>"
<?php
if ($advisor_emp->num_rows()>0 )
{
$i=1;
foreach($advisor_emp->result() as $data)
{
$eid = $data->id;
$cn = $data->company_name;
$fd = $data->from_date;
$td = $data->to_date;
$tb = $data->type_of_buisness;
$c1 = $data->company_addres_lane_1;
$c2 = $data->company_addres_lane_2;
$cc = $data->c_city;
$cp = $data->c_pincode;
$cs = $data->c_state;
$cun = $data->c_country; ?>
<div class="col-md-6">
<div class="form-group">
<input type="" name="eid[]" class="form-control" value="<?php echo $eid; ?>">
</div>
<div class="col-md-12">
<div class="form-group">
<label>Previous Employer</label>
<input type="text" class="form-control" name="pcmp" value="<?php echo $cn; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>From</label>
<input type="text" name="pcmpfrom" class="form-control" value="<?php echo $fd ; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>To</label>
<input type="text" name="pcmpto" class="form-control" value="<?php echo $td ; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Designation</label>
<input type="text" name="pcmpdesi" class="form-control" value="<?php echo $tb;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Company Address Line 1</label>
<input type="text" name="pcmpadr1[]" class="form-control" value="<?php echo $c1;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Company Address Line 2</label>
<input type="text" name="pcmpadr2" class="form-control" value="<?php echo $c2;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>City</label>
<input type="text" name="pcmpcit" class="form-control" value="<?php echo $cc;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Pin Code</label>
<input type="text" name="pcmppin[]" class="form-control" value="<?php echo $cp;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>State</label>
<input type="text" name="pcmpstat[]" class="form-control" value="<?php echo $cs; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Country</label>
<input type="text" name="pcmpcun[]" class="form-control" value="<?php echo $cun; ?>">
</div>
</div>
</div>
<?php
$i++;
}
}
?>
<div class="col-md-12"
<input type="submit" class="btn btn-primary" name="advisor_update" value="Update" >
</div>
</form>
Here is my Controller
In controller i'm goin to getting data from the view using for loop and sending recieved data to modal.
if ($this->input->post('advisor_update'))
{
$emp_his_id = $this->input->post('eid');
$count_eid = count($emp_his_id);
for($i=0;$i<$count_eid;$i++)
{
$emp_his_pk = $emp_his_id[$i];
$inserte['company_name']= $this->input->post('cn');
$inserte['from_date']= $this->input->post('fd');
$inserte['to_date']= $this->input->post('td');
$inserte['type_of_buisness']= $this->input->post('tb');
$inserte['company_addres_lane_1']= $this->input->post('c1');
$inserte['company_addres_lane_2']= $this->input->post('c2');
$inserte['c_city']= $this->input->post('cc');
$inserte['c_pincode']= $this->input->post('cp');
$inserte['updated_on'] = date("Y-m-d");
$up_emp_history=$this->Frontend_model->update_employe($inserte=array(),$emp_his_pk);
}
}
Here is my Modal
function update_employe($inserte=array(),$emp_his_pk)
{
$this->db->where('id',$emp_his_pk);
$result=$this->db->update("is_employment", $inserte);
return $result;
}
View Code
I have change your view codes input name
<form method="post" action="<?php echo site_url('Home/advisor_update'); ?>" >
<?php
if ($advisor_emp->num_rows()>0 )
{
foreach($advisor_emp->result() as $data)
{
$eid = $data->id;
$cn = $data->company_name;
$fd = $data->from_date;
$td = $data->to_date;
$tb = $data->type_of_buisness;
$c1 = $data->company_addres_lane_1;
$c2 = $data->company_addres_lane_2;
$cc = $data->c_city;
$cp = $data->c_pincode;
$cs = $data->c_state;
$cun = $data->c_country; ?>
<div class="col-md-6">
<div class="form-group">
<input type="" name="eid[]" class="form-control" value="<?php echo $eid; ?>">
</div>
<div class="col-md-12">
<div class="form-group">
<label>Previous Employer</label>
<input type="text" class="form-control" name="cn[]" value="<?php echo $cn; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>From</label>
<input type="text" name="fd[]" class="form-control" value="<?php echo $fd ; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>To</label>
<input type="text" name="td[]" class="form-control" value="<?php echo $td ; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Designation</label>
<input type="text" name="tb[]" class="form-control" value="<?php echo $tb;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Company Address Line 1</label>
<input type="text" name="c1[]" class="form-control" value="<?php echo $c1;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Company Address Line 2</label>
<input type="text" name="c2[]" class="form-control" value="<?php echo $c2;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>City</label>
<input type="text" name="cc[]" class="form-control" value="<?php echo $cc;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Pin Code</label>
<input type="text" name="cp[]" class="form-control" value="<?php echo $cp;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>State</label>
<input type="text" name="cs[]" class="form-control" value="<?php echo $cs; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Country</label>
<input type="text" name="cun[]" class="form-control" value="<?php echo $cun; ?>">
</div>
</div>
</div>
<?php
}
}
?>
Controller Code
You have to run a foreach loop in controller.
if ($this->input->post('advisor_update'))
{
$emp_his_id = $this->input->post('eid');
$cn = $this->input->post('cn');
$fd = $this->input->post('fd');
$td = $this->input->post('td');
$td = $this->input->post('tb');
$c1 = $this->input->post('c1');
$c2 = $this->input->post('c2');
$cc = $this->input->post('cc');
$cp = $this->input->post('cp');
foreach($emp_his_id as $key => $eid)
{
$emp_his_pk = $eid;
$inserte = array();
$inserte['company_name']= $cn[$key];
$inserte['from_date']= $fd[$key];
$inserte['to_date']= $td[$key];
$inserte['type_of_buisness']= $tb[$key];
$inserte['company_addres_lane_1']= $c1[$key];
$inserte['company_addres_lane_2']= $c2[$key];
$inserte['c_city']= $cc[$key];
$inserte['c_pincode']= $cp[$key];
$inserte['updated_on'] = date("Y-m-d");
$up_emp_history=$this->Frontend_model->update_employe($inserte=array(),$emp_his_pk);
}
}
Hope This works for you

Can not get data from database in ECHO

My edit.php code is stated below, and this is what the outcome is: http://i.imgur.com/lt89Wi8.png
But what it is supposed to do is, get the information from the username - loggin_session, and show their server details , so they dont have to re-write everything.
see: http://i.imgur.com/fGEsXPq.png
<?php
$query = "SELECT id, username, name, url, banner, description, sponsor, votes FROM websites WHERE username = '$login_session'";
$result = mysql_query($query) OR die($mysql_error());
$num = mysql_num_rows($result);
if ($num < 1) {
$n = FALSE;
echo '<font color="red">You have no servers to edit, You can add one here</font><br />';
die();
} ?>
<form action="" method="post" name="join_form" class="form-horizontal" role="form" enctype="multipart/form-data" onSubmit="return checkform(this);">
<div class="form-group ">
<label for="join_email" class="col-md-1 control-label"><span class="required">*</span>Server Title</label>
<div class="col-md-5">
<input name="name" value="<?php echo $_POST['name']; ?>" class="form-control" placeholder="<?php echo ''.$row['name'].''?>" required>
</div>
</div>
<div class="form-group ">
<label for="join_password" class="col-md-1 control-label"><span class="required">*</span>Website URL</label>
<div class="col-md-5">
<input name="url" value="<?php if (isset($_POST['url'])) echo $_POST['url']; ?>" class="form-control" placeholder="<?php echo ''.$row['url'].''?>" required>
</div>
</div>
<div class="form-group ">
<label for="join_url" class="col-md-1 control-label"><span class="required">*</span>Banner URL</label>
<div class="col-md-5">
<input name="banner" value="<?php if (isset($_POST['banner'])) echo $_POST['banner']; ?>" class="form-control" type="text" placeholder="<?php echo ''.$row['banner'].''?>" required>
</div>
</div>
<div class="form-group ">
<label for="join_title" class="col-md-1 control-label"></label>
<div class="col-md-5"></div>
</div>
<div class="form-group ">
<label for="join_description" class="col-md-1 control-label"><span class="required">*</span>Descr..</label>
<div class="col-md-5">
<textarea cols="50" rows="5" value="<?php if(isset($_POST['description'])) echo $_POST['description']; ?>" name="description" id="description_size" class="form-control" placeholder="<?php echo ''.$row['description'].''?>" required></textarea>
</div>
</div>
<input type="submit" name="submit" value="Add" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
$row is undefined in your code. It has to be
$row=mysql_fetch_assoc($result); // fetch it from the result set
How can I prevent SQL injection in PHP?
And what's up with that useless '' in every echo?
echo ''.$row['name'].''
^^ ^^
This is enough
echo $row['name'];

Not Fetch All column in the data in database and only one can retrieve

Hi everyone I have a problem with my query..
When I try To a data one by one same result and the result that retrieve is on the last id of the database
This is my codes..
$code = $mysqli->prepare("SELECT id,code FROM table_inventory");
$code->execute();
$code->bind_result($id,$ccode);
$res = $code->get_result();
while ($row = $res->fetch_array()){
$id = $row['id'];
$co = $row['code'];
}
<form role="form">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="test" class="form-control" id="exampleInputEmail3" placeholder="Enter email">
</div>
<div class="form-group">
<label for="code">Item Code</label>
<input type="text" class="form-control" id="code" placeholder="code" value="<?php echo $co; ?>">
</div>
Thank in advance.
Not tested, but something like this:
while ($row = $res->fetch_array()){
$id[] = $row['id'];
$co[] = $row['code'];
}
<form role="form">
<?php
foreach($id as $ind=>$val){ ?>
<input type="hidden" name="id" value="<?php echo $id[$ind]; ?>">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="test" class="form-control" id="exampleInputEmail3" placeholder="Enter email">
</div>
<div class="form-group">
<label for="code">Item Code</label>
<input type="text" class="form-control" id="code" placeholder="code" value="<?php echo $co[$ind]; ?>">
</div>
<?php
}
?>
I'm guessing this isn't your full code by the way, because your PHP and HTML are mixed in.

How to stop form submission after validation has suceeded

I was wondering if there was a way I could stop the form from being submitted normally after Jquery validation has actually succeeded.
I would like some other function to take care of submitting the form. I'd like the form to be validated but not submitted. Hope anyone can help
I'm using the VarienForm validation class.
here'a an example below:
var newform = new VarienForm('newform', true);
Thanks in advance.
here's my form:
<form id="newform" >
<div class="fieldset">
<h2 class="legend">User Details</h2>
<ul class="form-list">
<li class="fields">
<div class="field">
<label for="firstname" class="required"><em>*</em>First Name</label>
<div class="input-box">
<input name="firstname" id="firstname" title="First Name of Staff" value="<?php if (isset($user->firstname)){ echo $user->firstname; } ?>" class="input-text required-entry" type="text" tabindex="1"/>
</div>
</div>
<div class="field">
<label for="lastname" class="required"><em>*</em>Last Name</label>
<div class="input-box">
<input name="lastname" id="lastname" title="Last Name of Staff" value="<?php if (isset($user->lastname)){ echo $user->lastname; } ?>" class="input-text required-entry" type="text" tabindex="2"/>
</div>
</div>
</li>
<li class="fields">
<div class="field">
<label for="othername" class="required"><em>*</em>Other Name (s)</label>
<div class="input-box">
<input name="othername" id="othernames" title="Other Name(s)" value="<?php if (isset($user->othername)){ echo $user->othername; } ?>" class="input-text" type="text" tabindex="3"/>
</div>
</div>
<div class="field">
<label for="phone" class="required"><em>*</em>Phone Number</label>
<div class="input-box">
<input name="phone" id="phone" title="Phone Number" value="<?php if (isset($user->phone)){ echo $user->phone; } ?>" class="input-text validate-number" type="text" tabindex="4"/>
</div>
</div>
</li>
<li class="fields">
<div class="field">
<label for="username" class="required"><em>*</em>User Name</label>
<div class="input-box">
<input name="username" id="username" title="User Name" value="<?php if (isset($user->username)){ echo $user->username; } ?>" class="input-text required-entry" type="text" tabindex="5" />
</div>
</div>
<div class="field">
<label for="email" class="required"><em>*</em>Email</label>
<div class="input-box">
<input name="email" id="email" title="User Email" value="<?php if (isset($user->email)){ echo $user->email; } ?>" class="input-text validate-email" type="text" tabindex="6" />
</div>
</div>
</li>
<li class="fields">
<div class="field">
<label for="password" class="required"><em>*</em> Password</label>
<div class="input-box">
<input name="password" id="password" title="Password" value="<?php // if (isset($user->password2)){ echo $user->password2d; } ?>" class="input-text validate-password required-entry" type="password" tabindex="7"/>
</div>
</div>
<div class="field">
<label for="password2" class="required"><em>*</em>Confirm Password</label>
<div class="input-box">
<input name="password2" id="password" title="Confirm Password" value="<?php // if (isset($user->password2)){ echo $user->password2; } ?>" class="input-text validate-password required-entry validate-cpassword" type="password" tabindex="8"/>
</div>
</div>
</li>
<li class="fields">
<div class="field">
<label for="Role" class="required"><em>*</em>Role</label>
<div class="input-box">
<?php $roles = Role::find_all(); if ($roles){ ?>
<select name="role" id="role" class="required-entry" tabindex="9" value="">
<option value="" selected="selected">SELECT ROLE</option>
<?php foreach ($roles as $role) : ?>
<option value="<?php echo $role->id; ?>"><?php echo $role->name ?></option>
<?php Endforeach; ?>
</select>
<?php } else { echo "No Roles Found! Add Role"; } ?>
</div>
</div>
<div class="field">
<label for="Status" class="required"><em>*</em>Status</label>
<div class="input-box">
<select name="status" id="status" class="required-entry" tabindex="10" value="">
<option value="1" selected="selected">Active</option>
<option value="0">Inactive</option>
</select>
</div>
</div>
</li>
</ul>
<div class="buttons-set">
<input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
<input type="submit" name="submit" value="SAVE" class="buttons-set" />
</div>
</div>
</form>
Since you tagged jQuery, I'm going to give you a jquery solution:
$("#newform").submit(function() {
return false;
});
Similarly, you can use this variation (actually this is the right way to do it)
$(document).ready(function() {
// I'm not familiar with VarienForm
// but the statement below runs once all DOM elements
// become available.
var newform = new VarienForm('newform', true);
// The block of code below only runs after you click
// the submit button in your form with id="newForm"
$("#newform").submit(function(e) {
// you can put a condition here, for example:
// if your form validation is correct return true;
// otherwise run e.preventDefault()
e.preventDefault();
});
});
You need to return false in your "onSubmit" method.
More details are explained in this similar post: How to prevent buttons from submitting forms
You can give the form an onsubmit="function" and in the function do an return false, before you return the false run another function.
First take a look at code i wrote below :
var x=true;
$('#newform').submit(function(e){
if(x) e.preventDefault();
myfun($(this));
});
function myfun(p) {
//lots of work...(simulating with timeout it could be anything ie. 2*2*2...)
setTimeout(function(){
x=false;
p.submit();
},1000);
}
Link to demo: >>> http://jsfiddle.net/techsin/QwWfb/
Main idea stop execution the time you want it to stop...but when ready to submit after doing whatever, let the stopping function know that it's not needed anymore. So when you call submit it actually goes through.
Control x to control submitting.

Reset button using JavaScript is not working after validating the form using Ajax

I have a "create user"form with reset and create button. This reset button is not working for some drop-downs in the form.
In addition, reset button is not clearing all the values in the form after validation. It only clears the values entered after validation.
Here is my code:
<div class="form-action clearfix">
<button type="button" onClick="clearForm()" > Reset </button>
<button class="button" type="button" id="button" onclick="doAjax('index.php/users/createUser', convertFormJson('#createUserForm'));">Create</button>
and
function clearForm() {
document.getElementById('createUserForm').reset();
}
Form:
<?php echo validation_errors(); ?>
<div class="container_12 clearfix leading" >
<div class="grid_12">
<!--<form class= "form has-validation">-->
<?php
$attributes = array('class' => 'form has-validation', 'id' => 'createUserForm');
echo form_open('/users/createUser', $attributes);
?>
<div class="clearfix">
<label for="form-name" class="form-label">First Name <em>*</em></label>
<div class="form-input">
<input type="text" id="form-name" name="firstname" required="required" placeholder="Enter the first name" value="<?php echo set_value('firstname'); ?>" />
</div>
</div>
<div class="clearfix">
<label for="form-name" class="form-label">Last Name <em>*</em></label>
<div class="form-input">
<input type="text" id="form-name" name="lastname" required="required" placeholder="Enter the last name" value="<?php echo set_value('lastname'); ?>"/>
</div>
</div>
<div class="clearfix">
<label for="form-email" class="form-label">Email <em>*</em></label>
<div class="form-input"><input type="email" id="form-email" name="email" required="required" placeholder="A valid email address" value="<?php echo set_value('email'); ?>"/></div>
</div>
<!--
<div class="clearfix">
<label for="form-birthday" class="form-label">Birthdate</label>
<div class="form-input"><input type="date" id="form-birthday" name="date" placeholder="mm/dd/yyyy" value="<?php //echo set_value('date'); ?>" class="date"/></div>
</div>
-->
<div class="clearfix">
<label for="form-username" class="form-label">Username <em>*</em></label>
<div class="form-input">
<?php echo form_error('username'); ?>
<input type="text" id="form-username" name="username" value="<?php echo set_value('username'); ?>" required="required" maxlength="30" placeholder="Alphanumeric (max 30 char.)" />
</div>
</div>
<div class="clearfix">
<label for="form-password" class="form-label">Password <em>*</em></label>
<div class="form-input">
<?php echo form_error('password'); ?>
<input type="password" id="form-password" name="password" value="<?php echo set_value('password'); ?>" maxlength="30" placeholder="Min 8 char. containing a capital, number and/or symbol" />
</div>
</div>
<div class="clearfix">
<label for="form-password-check" class="form-label">Password check <em>*</em></label>
<div class="form-input">
<?php echo form_error('passconf'); ?>
<input type="password" id="form-password-check" name="passconf" value="<?php echo set_value('passconf'); ?>" data-equals="password" maxlength="30" placeholder="Re-enter your password" />
</div>
</div>
<div class="clearfix">
<label for="form-group" class="form-label">Group <em>*</em></label>
<div class="form-input">
<?php
$groupsData[' '] = "--";
$groupJs = 'id="form-group"';
echo form_dropdown('groupId', $groupsData,' ',$groupJs);
?>
</div>
</div>
<div class="clearfix">
<label for="form-language" class="form-label" >Language <em>*</em></label>
<div class="form-input">
<select id="form-language" name="language">
<option value ="" > -- </option>
<option value ="1">English</option>
<option value ="2">French</option>
</select>
</div>
</div>
<div class="clearfix">
<label for="form-timezone" class="form-label" >Timezone <em>*</em></label>
<div class="form-input">
<?php echo timezone_menu('UM5', 'form-timezone'); ?>
</div>
</div>
Please help me out.

Categories