Trying to build a simple calculator in codeigniter - php

I'm trying to build a simple addition calculator in CodeIgniter. I am trying to get value from the post method into my controller. While executing the code, I'm getting an error saying
undefined index: number1
My controller file:
public function addQuote(){
if(isset($_POST['adds'])){
$ans=$_POST['number1'] + $_POST['number2'];
$data= array(
'number1'=> $_POST['number1'],
'number2'=> $_POST['number2'],
'ans'=> $ans
);
} else{
$data = array(
'number1'=> "0",
'number2'=> "0",
'ans'=> '0'
);
}
$this->load->view('addQuote',$data);
My view file is here:
<div class="col-md-3 col-md-offset-4 well">
<h2> Addition of two numbers</h2>
<form action="<?php echo base_url(''); ?>welcome/addQuote" method="post">
<div class="form-group">
<label for="number1"> Enter number 1</label>
<input type="number" class="form-control" id="number1" placeholder="Enter number 1" value="<?php $number1;?>">
</div>
<div class="form-group">
<label for="number2"> Enter Number</label>
<input typle="number" class="form-control" id="number2" placeholder="Enter number 2" value="<?php $number2;?>">
</div>
<div class="form-group">
<label for="ans"> Answer</label>
<p class="text-success"><?= $ans; ?> </p>
</div>
<button type="submit" class="btn btn-default" name="adds" > submit</button>

You need to add name attribute to your elements.
As only elements are submitted with name attribute added.
id and class are mainly for CSS and JS purposes.
So,
<input type="number" class="form-control" id="number1" placeholder="Enter number 1" value="<?php $number1;?>">
Should be:
<input type="number" class="form-control" id="number1" placeholder="Enter number 1" value="<?php $number1;?>" name="number1">
Observe the added name attribute.
Same for number2

<input type="number" class="form-control" id="number1" placeholder="Enter number 1" value="<?php $number1;?>" name="number1">
<input type="number" class="form-control" id="number2" placeholder="Enter number 1" value="<?php $number2;?>" name="number2">
<button type="submit" class="btn btn-default" name="adds" > submit</button>

Related

How to Save batch users in laravel 8.0?

I created a form, where one can add or remove entries and save all entries at once.
Now the form only saves the last entry. Scripts to add and remove record, works pretty well.
How can i make sure all the entries are saved to the db. I have been looking for a solution but can't find any.
Controller
`public function saveGroupDelegateData(Request $request ){
foreach ($request -> (*.lastIndex) as $key => $lastIndex) {
$SaveAttendees = Attendees::create([
'title' => $request->title[$lastIndex],
'name' => $request->name[$lastIndex],
'email' => $request->email[$lastIndex],
'company' => $request->company[$lastIndex],
'phone_number' => $request->phone_number[$lastIndex],
'job_title' => $request->job_title[$lastIndex],
'event_role' => $request->event_role[$lastIndex],
'tour' => $request->tour[$lastIndex],
'tour_location' => $request->tour_location[$lastIndex]
]);
}}`
Viewblade
`<form action="/SaveGroupAttendees" method="POST" enctype="multipart/form-data">
#csrf
<div id="inputFormRow" >
<p style="background-color:DodgerBlue;">Attendee [1]</p>
<label>Email to share Invoice to</label><input type="text" name="desired_mail" placeholder="Email to Share Invoice To" />
<br> <br>
<div class="input-group mb-3">
<input type="hidden" name="Users.Index" value="1" />
<input type="text" required="required" placeholder="title" name="title[1]">
<input type="text" required="required" placeholder="Full name" name="name[1]">
<input type="text" required="required" placeholder="email" name="email[1]">
<input type="text" required="required" placeholder="company" name="company[1]">
<input type="text" required="required" placeholder="phone_number" name="phone_number[1]">
<input type="text" required="required" placeholder="job_title" name="job_title[1]">
<input type="hidden" value="delegate" placeholder="event_role" name="event_role[1]">
<label for="tour[1]" >Attend Excursion</label>
<select name="tour[1]" class="form-control round" id="tour" >
<option value="yes">Yes</option>
<option active value="no">No</option>
</select>
<label id="tour_location" for="tour_location[1]" >Exursion Location</label>
<select class="form-control round" id="tours" name="tour_location[1]">
<option value="None">Choose Location</option>
<option value="kenya">Kenya</option>
<option value="uganda">Uganda</option>
</select>
<div class="input-group-append">
<!-- <button id="removeRow" type="button">
(-) Remove Attendee
</button> -->
</div>
</div>
</div>
<div id="newRow"></div>
<p>
<button id="addRow" type="button">
(+) Add Attendee
</button>
</p>
<div class="text-center">
<input class="btn btn-danger" type="submit" value="{{ trans('global.save') }}">
</div>
</form>
</div>
</div>
</d iv>
<i class="fa fa-angle-up"></i>
<script type="text/javascript">
// add row
let lastIndex= 2 ;
// add row
$("#addRow").click(function () {
var html = '';
html += '<div id="inputFormRow" class="table table-condensed table-striped">';
html += ' <p style="background-color:DodgerBlue;">Attendee['+lastIndex+']</p>';
html += ' <div class="input-group mb-3">';
html += ' <input type="text"required="required" placeholder="title" name="title['+lastIndex+']">';
html += ' <input type="text" required="required" placeholder="Full Name" name="name['+lastIndex+']">';
html += ' <input type="text" required="required" placeholder="Email" name="email['+lastIndex+']">';
html += ' <input type="text" required="required" placeholder="Company" name="company['+lastIndex+']">';
html += ' <input type="text" required="required" placeholder="Phone Number" name="phone_number['+lastIndex+']">';
html += ' <input type="text" required="required" placeholder="Job title" name="job_title['+lastIndex+']"';
html += ' <input type="hidden" value="delegate" placeholder="event_role" name="event_role['+lastIndex+']">';
html += ' <label for="tour['+lastIndex+']">Attend Excursion?</label> <select class="form-control round" id="tour" name="tour['+lastIndex+']"><option value="yes">Yes</option><option active value="no">No</option></select>';
html += ' <label for="tour_location['+lastIndex+']">Excursion Location</label><select class="form-control round" name="tour_location['+lastIndex+']" id="tours"><option active value="None" >Choose Location</option><option value="KE">Kenya</option><option value="Uganda">Uganda</option></select>';
html += ' <div class="input-group-append">';
html += ' <button id="removeRow" type="button">';
html += ' (-) Remove Attendee';
html += ' </button>';
html += ' </div>';
html += ' </div>';
html += '</div>';
lastIndex=lastIndex+1;
$('#newRow').append(html);
});`
// remove row
$(document).on('click', '#removeRow', function () {
lastIndex=lastIndex-1;
$(this).closest('#inputFormRow').remove();
});
i think you can change the name of the input element. you can change it to this:
<input type="text" required="required" placeholder="title" name="data[lastIndex][title]">
and in controller you can looping the data.
foreach($request->data as $value) {
Attendees::create([
'title' => $value['title']
]);
}

Pass value between 2 html forms in the same page

this question has been asked but I haven't found a working solution.
I have 2 forms. The first is sending datas to the database (form2). The second one is uploading a photo (form1).
I would need to save the path of the uploaded picture and store it in a text input in form1 and after send it with the form2 datas. Do you have any tips? It is possible without JQuery using just Php?
I have a solution with JQuery but it is not working.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
function submitform()
{
var name=$('#name').val();
$('#variable').val(name); // set the value of name field to the hidden field
$('form#form1').submit(); // submit the form
}
</script>
Create a new product
<form action="../handlers/processNewProduct.php" id="form2">
<div class="mb-3">
<label for="productname" class="form-label">Product Name</label>
<input type="text" class="form-control" id="productname" name="productname" placeholder="Product Name">
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<input type="text" class="form-control" id="description" value="xxxxxxxxx" name="description" placeholder="Description">
</div>
<div class="mb-3">
<label for="pricelist" class="form-label">Price list</label>
<input type="number" class="form-control" id="pricelist" name="pricelist" placeholder="Price">
</div>
<input type='text' name='variable' id='variable' value=''>
<button type="" class="btn btn-primary">Insert data</button>
</form>
<form action="../handlers/processUploadProductPicture.php" method="post" enctype="multipart/form-data" id="form1" onsubmit="submitform()">
Select image to upload:
<input type='file' name='fileToUpload' id='name'>
<input type='submit' value='Upload Image' id='upload'>
</form>

How to get particular columns from database

I want only 4 columns from database in below
I have to get only 4 column from database for displaying in textfield please give suggestion for that. The table is shown below thank you
$sql_getnm = "SELECT * FROM util WHERE util_head IN ('wc_email', 'wc_contact_us', 'wc_mobile', 'wc_google_map');";
$result_getnm = $connect->query($sql_getnm);
while($row_getnm = $result_getnm->fetch_array())
$util_value_email_data=?
$util_value_mobile_data=?;
$util_value_map_data=?;
$util_value_data=?;
html form
<form id="submitForm" method="post" role="form" name="hl_form" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="mobile_number">Email*</label>
<input class="form-control" id="util_value_email" name="util_value_email" value="<?Php echo $util_value_email_data; ?>" maxlength="250" placeholder="Enter Email Address" type="text">
</div>
<div class="form-group">
<label for="mobile_number">Mobile*</label>
<input class="form-control" id="util_value_mobile" name="util_value_mobile" value="<?Php echo $util_value_mobile_data; ?>" maxlength="250" placeholder="Enter Mobile Number" type="text">
</div>
<div class="form-group">
<label for="mobile_number">Map*</label>
<input class="form-control" id="util_value_map" name="util_value_map" value="<?Php echo $util_value_map_data; ?>" maxlength="250" placeholder="Enter Map Address" type="text">
</div>
<div class="form-group">
<label for="description" class="required">Description*</label>
<textarea class="form-control" style="resize: none;" id="util_value" name="util_value" rows="3" placeholder="Enter Description"><?Php echo $util_value_data; ?></textarea>
</div>
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</form>
Below is the code from which you can get all the four data that you want.
$sql_getnm = "SELECT * FROM util WHERE util_head IN ('wc_email', 'wc_contact_us', 'wc_mobile', 'wc_google_map');";
$result_getnm = $connect->query($sql_getnm);
while($row_getnm = $result_getnm->fetch_array()) {
if($row_getnm['util_head'] == 'wc_email'){
$util_value_email_data = $row_getnm['util_value'];
}
if($row_getnm['util_head'] == 'wc_contact_us'){
$util_value_contact_data = $row_getnm['util_value'];
}
if($row_getnm['util_head'] == 'wc_mobile'){
$util_value_mobile_data = $row_getnm['util_value'];
}
if($row_getnm['util_head'] == 'wc_google_map'){
$util_value_map_data = $row_getnm['util_value'];
}
}
You need to replace * with columns name with comma separation.
$sql_getnm = "SELECT Columname1,Columname2,Columname3,Columname4 FROM util WHERE util_head IN ('wc_email', 'wc_contact_us', 'wc_mobile', 'wc_google_map');";
$result_getnm = $connect->query($sql_getnm);
while($row = mysql_fetch_array($result_getnm, MYSQL_ASSOC)
$util_value_email_data= $row['Columname1'];
$util_value_mobile_data= $row['Columname2'];
$util_value_map_data= $row['Columname3'];
$util_value_data= $row['Columname4'];

Duplicate Form using button

I'm trying to duplicate 4 input text. For example I want to duplicate 'Produk Simpanan Saham', then only 4 input on 'Produk Simpanan Saham' div will be duplicated. I already tried a few methods using jQuery but it seem doesn't work.
<form action="" method="post" class="form-horizontal">
<br><br>
<div class="form-group">
Upload File :
<input type="file" name="file">
</div>
<div class="saham">
<div class="w3-container w3-black">
<h3>
<center>Produk Simpanan Saham</center>
</h3>
</div>
<label for="kp_produk">Kode Produk :</label>
<input type="text" name="kp_produk" class="form-control">
<br>
<label for="produk_saham">Produk :</label>
<input type="text" name="produk_saham" class="form-control">
<br>
<label for="bunga_saham">Bunga :</label>
<input type="text" name="bunga_saham" class="form-control">
<br>
<label for="ket_saham">Keterangan :</label>
<input type="text" name="ket_saham" class="form-control">
</div>
<div class="harian">
<div class="w3-container w3-black">
<h3>
<center>Produk Simpanan Harian</center>
</h3>
</div>
<label for="kp_harian">Kode Produk :</label>
<input type="text" name="kp_harian" class="form-control">
<br>
<label for="produk_harian">Produk :</label>
<input type="text" name="produk_harian" class="form-control">
<br>
<label for="bunga_harian">Bunga :</label>
<input type="text" name="bunga_harian" class="form-control">
<br>
<label for="ket_harian">Keterangan :</label>
<input type="text" name="ket_harian" class="form-control">
</div>
you mush have blueprint of field first like
var blueprint= '<div class="saham">'+
'<div class="w3-container w3-black">'+
'<h3>'+
'<center>Produk Simpanan Saham</center>'+
'</h3>'+
'</div>'+
'<label for="kp_produk">Kode Produk :</label>'+
'<input type="text" name="kp_produk[]" class="form-control">'+
'<br>'+
......
used [] for all name field to get array request when you send form to server
example :
'<input type="text" name="kp_produk[]" class="form-control">'+
and make button add like (jquery) example
<button onclick="$('form').append(blueprint)">add</button>
just use
var dom=$($('.saham')[0]).clone();
//copy dom
$('.form-horizontal').html(dom):
//paste dom
dont forget to add [] on name tag dom input to set data send to array, because if you not set. data with key kp_produk value is last tag name with name kp_produk
if you use [] (kp_product[]) data will set to array
kp_product=['anu1',anu2];

Unable to submit form using jquery submit method

What i am trying to do is:
post a form when user is logged in.
but if he is not logged in then pop up login is shown to user.
and in that popup redirection URL is added to hidden field.
when popup opens and i login it redirect me to that form.
But when i try to submit form it not being submitted.
// submit button in form
$('#submitcompanyEnquiry').on('click',function(e){
e.preventDefault();
//get data attr to check if user is login
if($('#companyEnquiry').data('login')){
//companyEnquiry =>form id
//here i try to submit form
console.log('testing'); --->it is working
jQuery('#companyEnquiry').submit(); ---> //the problem is here this piece of code is executing
}else{
if($('#companyEnquiry').attr('action')!=''){
//here i added the current url to hidden field latter to used for redirection
$('#loginForm #redirectUrl').val($('#companyEnquiry').data('seotitle'));
}
//here the login popup is trigger.
jQuery("#login").trigger('click');
}
});
Things that I confirmed:
ensure that there is unique id with the
name provided.
console some value in the if block which was
running but the line of code i have mention.
PHP part is working fine i have removed the e.preventDefault();
it is works fine but doesn't achieve the require functionality.
HTML Code
<form action="<?=Route::url('default',array('controller'=>'contact','action'=>'user_contact'))?>" data-login="<?php echo $data; ?>" data-seotitle="<?=Route::url('company', array('controller'=>'listing','seotitle'=>$company_seotitle))?>" id="companyEnquiry" method="post">
<input type="hidden" name="company_to" value="<?php echo $id; ?>">
<?php if (!$auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="Name" name="name" required aria-describedby="basic-addon1">
</div>
<?php }else { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="Name" required value="<?php echo $auth->get_user()->company_name; ?>" name="name" aria-describedby="basic-addon1">
</div>
<?php } ?>
<?php if (!$auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="email" class="form-control search" placeholder="email" required name="company_from" aria-describedby="basic-addon1">
</div>
<?php }else { ?>
<div class="input-group searchbox">
<input type="email" class="form-control search" placeholder="email" required value="<?php echo $auth->get_user()->companyemail; ?>" name="company_from" aria-describedby="basic-addon1">
</div>
<?php } ?>
<?php if ($auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="phone number" required name="phone" value="<?php echo $auth->get_user()->company_phone_1; ?>" aria-describedby="basic-addon1">
</div>
<?php } else { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="phone number" required name="phone" aria-describedby="basic-addon1">
</div>
<?php } ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="subject" required name="subject" aria-describedby="basic-addon1">
</div>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="message" required name="message" aria-describedby="basic-addon1">
</div>
<input data-login="<?php echo $data; ?>" id="submitcompanyEnquiry" type="submit" name="submit" value="SEND" class="form-control blue-btn send-btn">
</form>
The problem can only exist in the way you are adding the login variable to the form with the id : companyEnquiry
Check if you have add it as correct parameter because jquerys data function will only read values which have a "data-" tag infront of it.
So your php code should look like this :
echo '<form id="companyEnquiry" ' . ($login ? 'data-login="1"' : '' . '>

Categories