Append table using jQuery with php - php

Hi every one I'm trying to append a table within a form. But i'm having an error message in console: "Uncaught SyntaxError: missing ) after argument list". When I click the button to add rows it keeps directing me to php script. I Tried to fix it but no luck anyway sorry for bad english, here are the codes.
$('#more_row').append('<tr id="row'+i+'"><td><?php echo "<select name='alpha_num[]' class='form-control' id='item'>"; echo"<option value=''selected disabled>Select Material</option>";while($row = mysqli_fetch_row($result1)){echo"<option value=$row[0]>$row[1]$row[0]</option>";}echo"</select>";?></td><td><input type="text" name="qty[]" class="form-control" id="quantity" placeholder="Qty"></td><td><input type="text" name="uom[]" class="form-control" id="um" placeholder="UOM"></td><td><select name="status[]" class="form-control" id="stat"><option value="approved">approved</option><option value="return">return</option></select></td><td><button id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
<div class="panel-body">
<form method="post" action="material_receive_report_process.php">
<div class="row">
<div class="col">
<label for="dr">PO.NO</label>
<input type="text" name="dr_no" class="form-control" id="dr">
</div>
<div class="col">
<label for="dr">DR.NO</label>
<input type="text" name="dr_no" class="form-control" id="dr">
</div>
<div class="col">
<label for="date">Date</label>
<input type="text" name="date" class="form-control" id="date">
</div>
</div>
<table id="more_row">
<tr>
<td>
<?php echo "<select name='alpha_num[]' class='form-control' id='item'>";
echo"<option value=''selected disabled>Select Material</option>";
while($row = mysqli_fetch_row($result1)){
echo"<option value=$row[0]>$row[1]$row[0]</option>";
}
echo"</select>";
?>
</td>
<td>
<input type="text" name="qty[]" class="form-control" id="quantity" placeholder="Qty">
</td>
<td>
<input type="text" name="uom[]" class="form-control" id="um" placeholder="UOM">
</td>
<td>
<select name="status[]" class="form-control" id="stat">
<option value="approved">approved</option>
<option value="return">return</option>
</select>
</td>
<td><button id="add1" class="btn btn-info">Add</button></td>
</tr>
</table>
<br>
<p>Condition:
<input type="checkbox" name="condition[]" value="damage_visible">Damage visible
<input type="checkbox" name="condition[]" value="good_condition">Good conditon
<input type="checkbox" name="condition[]" value="full_qty">Full qty
<input type="checkbox" name="condition[]" value="partial_qty">Partial qty</p>
<div class="form-group">
<label for="supp">Supplier</label>
<input type="text" name="supplier" class="form-control" id="supp">
</div>
<div class="form-group">
<label for="remark">Remarks</label>
<textarea name="remark" class="form-control" id="remark"></textarea></p>
<input type="submit" name="save" class="btn btn-success" value="Save">
</div>
</form>
<br><br>
</div>
</div>

The 'add row' button submits the form because the default value for the type attribute of button elements is "submit". My guess is that the syntax error is due to the mess of single quotes, double quotes, and line breaks that PHP is dumping in your Javascript. Look at the generated code your browser gets. That's what needs to be correctly formed.

The error is most likely due to conflict between Php and JavaScript strings. Consider escaping i.e. \' and using different one for the two say JavaScript use double quotes " while Php use single quotes '

Related

Update Angular Record

For some reason my mind is wrapping around what the problem is here. I am using Angular with a PHP mysql database. I have done console.log for basically every step of the way and it seems to be working, but the textboxes won't display the information that is already inserted into the database.
This form appears when the update button is pressed:
<div *ngIf="this.isClubEditVisible">
<form #f = "ngForm">
<br><br><br>
{{ account.account_type }}
<label>Club Name </label>
<input type="text" name="school_name" class="form-control input-sm" [(ngModel)]="selectedAccount.account_type" placeholder="Full School Name" autocomplete="off">
<br><br>
<h3>Contact Information</h3>
<label>Contact Full Name</label>
<input type="text" name="contact_name" class='form-control input-sm' [(ngModel)]="selectedAccount.contact_name" placeholder="John Doe">
<br><br>
<label>Contact Email </label>
<input type="text" name="contact_email" class="form-control input-sm" [(ngModel)]="selectedAccount.contact_email" placeholder="name#example.com">
<br><br>
<label>Contact Phone Number </label>
<input type="text" name="contact_number" class="form-control input-sm" [(ngModel)]="selectedAccount.contact_number" placeholder="(000) 000-0000">
<br><br>
<input type="button" (click)="update(f)" class="btn btn-success" value="Update Account">
<input type="button" (click)=" cancel()" class="btn btn-cancel" value="Cancel">
</form>
</div>
This report is initiated by the update button which also calls a function to define what record to edit in the database.
<table border='0' width='100%' style='border-collapse: collapse;'>
<tr *ngFor="let Account_Info of account; let i = index">
<button (click)="selectPolicy(account);this.isClubEditVisible = true;" class="nav-delete" type="button">Edit Account Information</button>
</tr>
</table>
This is the function called by the edit button
selectPolicy(account: Account_Info){
console.log("selected account");
this.selectedAccount = account;
console.log(this.selectedAccount);
}
When I print out the selected account in the console it shows the array correctly. Then I thought the textboxes should update above to display the information correctly. But the textboxes are null. Any suggestions?

Is there any way to update and insert selected inputs to the database? Codeigniter

I have successfully inserted multiple values and now the PROBLEM is to query, how can I update at the same time to update the correct_id to 1 - How can I update the selected input using the radio button as a reference value.
Here is my UI, on how it was designed.
I have a field name correct_id which value needs to be 1 in order to be considered as a correct answer. Please guide me thorougly as this became a challenge to me. I became confused with the query recently thanks!
So as you can see I have radio buttons, I need to choose one input or choice to be my answer
Here is my Model.
public function addQuestion(){
// Insert questions
$field_question = array(
'question'=>$this->input->post('question'),
);
$this->db->insert('questions', $field_question);
// Insert Answers
$data_answer = $this->input->post('choice[]');
$value = array();
for($i = 0; $i < count($data_answer); $i++) {
$value[$i] = array(
'answer' => $data_answer[$i],
'question_id' => $this->db->insert_id(),
);
}
$this->db->insert_batch('answers', $value);
if($this->db->affected_rows() > 0){
return true;
}else{
return false;
}
}
And my Form in view
Here I'll precicely show the radio input and its name and value
<div class="form-check">
<input class="form-check-input" type="radio" name="checkChoice[]" id="checkChoice" value="1">
<label class="form-check-label" for="exampleRadios1">
Make this as an Answer
</label>
</div>
And here is the full view form code.
<form method="post" id="myForm" action="<?php echo base_url(); ?>posts/addQuestion">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">Question</span>
</div>
<input type="text" placeholder="Enter your Question" name="question" id="question" class="form-control" required />
</div>
<hr>
<h5>Create Choices: </h5>
<div class="input-group input-group-sm mb-3">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td><input type="hidden" name="choiceHid[]" value="0" /></td>
<td><input type="text" name="choice[]" id="choice" placeholder="Enter your Choice" class="form-control" /> </td>
<td><button type="button" name="add" id="add" class="btn btn-success"><span class="iconify" data-icon="ant-design:plus-circle-outlined" data-inline="false"></span> Add Response </button></td>
<td>
<div class="form-check">
<input class="form-check-input" type="radio" name="checkChoice[]" id="checkChoice" value="1">
<label class="form-check-label" for="exampleRadios1">
Make this as an Answer
</label>
</div>
</td>
</tr>
</table>
</div>
</div>
<hr>
<div class="col-md-8">
<h5> Your Answer:
<input type="hidden" name="txtAnswer" value="0" />
<input type="text" placeholder="Enter the Question's Answer" name="answer_choice" id="answer_choice" class="form-control mt-2" /><h5>
</div>
<hr>
<input type="button" id="btnSave" class="btn btn-block btn-info" value="Submit" />
</form>
First, assign a different index random id for each answer radio and text fields (Ex.: choice[n] and checkChoice[n] pair):
<input type="text" name="choice[13]" id="choice" placeholder="Enter your Choice" class="form-control" /> </td>
<input class="form-check-input" type="radio" name="checkChoice[13]" id="checkChoice" value="1">
After, use foreach and verify if the checkChoice with the same index that choice in the loop was sent:
foreach($data_answer as $index => $value) {
array_push($value, array(
'answer' => $value,
'question_id' => $this->db->insert_id(),
'correct_id' => ((isset($_POST['checkChoice'][$index])) ? 1 : null)
));
}
Because now each radio has a different name, radio behaves like a checkbox (allows you to choose more than one). So, you must force a radio behaviour again with a jquery snippet:
$( document ).ready(function() {
$('input[type=radio]').change(function() {
// When any radio button on the page is selected,
// then deselect all other radio buttons.
$('input[type=radio]:checked').not(this).prop('checked', false);
});
});

add value from new input field then sum with existing database value

i want to add stock, then it will sum with the database stock
the field for the total sum(jumlahtotal) didn't work
anyway, i already put js at the header template
<div class="content-wrapper">
<div class="panel-body">
<form action="" method="POST">
<?php echo validation_errors(); ?>
<input type="hidden" name="id" value="<?= $buku['id'];?>">
<input type="hidden" name="judul" value="<?= $buku['judul'];?>">
<input type="hidden" name="pengarang" value="<?= $buku['pengarang'];?>">
<input type="hidden" name="penerbit" value="<?= $buku['penerbit'];?>">
<input type="hidden" name="tahun_terbit" value="<?= $buku['tahun_terbit'];?>">
<input type="hidden" name="isbn" value="<?= $buku['isbn'];?>">
<input type="hidden" name="rak" value="<?= $buku['rak'];?>">
<input type="hidden" name="tgl_input" value="<?= $buku['tgl_input'];?>">
<div class="form-group">
<label for="jumlah">Jumlah Masuk</label>
<input type="number" class="form-control" id="masuk" name="jumlah" value="0">
<?= form_error('masuk','<small class="text-danger">','</small>') ?>
</div>
<div class="form-group">
<label>Jumlah Total</label>
<input type="number" class="form-control" id="jumlah" name="jumlah" value="<?= $buku['jumlah_buku']?>" readonly>
<?= form_error('jumlah','<small class="text-danger">','</small>') ?>
</div>
<div class="form-group">
<input type="number" id="jumlahtotal">
</div>
<button type="submit" name="ubah" class="btn btn-primary">Update Data</button>
</form>
</div>
</div>
<script>
$(document).ready(function() {
$('input').change(function () {
var total = 0;
$('input[name=jumlah]').each(function(){
total = total+parseInt($(this).val());
})
$('input[id=jumlahtotal]').html(total);
})
})
</script>
i already checked the js is running
You are trying to set the innerHTML of an input tag in your code
$('input[id=jumlahtotal]').html(total);
In HTML, input tag is an unpaired tag. It means you don't put anything inside <input> and </input>. It's just <input>. What you are trying is to put <input> someValue </input> but via jquery.
I think you want to do this instead
$('input[id=jumlahtotal]').val(total);
This just sets the value attribute of the input element. And it's the correct way to set the value to an input element.

How to add or remove values using jquery.?

<div class="container">
<div class="form-group">
<form>
<button>click to add/remove<input type="text" name="addfirst" value="10" readonly></button><br>
<button>click to add/remove<input type="text" name="addsecond" value="10" readonly></button><br>
<button>click to add/remove<input type="text" name="addthird" value="10" readonly></button><br>
<button>click to add/remove<input type="text" name="addfourth" value="10" readonly></button><br>
<label>total values</label>
<input type="text" name="total_ammount" value="50" readonly>
</form>
</div>
</div>
if i click button to add first input value which is 10 so it will addition with my total input field values if again the same button so substract the values.
remaining field should do the same work also.
how it is possible by using jquery please explain it.
Please Try the below code.,
function addValue(dhis){
var val=parseFloat(dhis.children().val()) // get the text box value
var total_ammount=parseFloat($('#total_ammount').val())+val
$('#total_ammount').val(total_ammount);
dhis.attr('onclick','subValue($(this))') // change the onclick function
}
function subValue(dhis){
var val=parseFloat(dhis.children().val()) // get the text box value
var total_ammount=parseFloat($('#total_ammount').val())-val
$('#total_ammount').val(total_ammount);
dhis.attr('onclick','addValue($(this))') // change the onclick function
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="form-group">
<form>
<button type="button" onclick="addValue($(this))">click to add/remove<input type="text" name="addfirst" value="10" readonly></button><br>
<button type="button" onclick="addValue($(this))">click to add/remove<input type="text" name="addsecond" value="10" readonly></button><br>
<button type="button" onclick="addValue($(this))">click to add/remove<input type="text" name="addthird" value="10" readonly></button><br>
<button type="button" onclick="addValue($(this))">click to add/remove<input type="text" name="addfourth" value="10" readonly></button><br>
<label>total values</label>
<input type="text" name="total_ammount" id="total_ammount" value="50" readonly>
</form>
</div>
</div>

Request not showing all data in array

I am using laravel 5.1 and I am in the middle of the problem.
<script type="text/javascript">
var counter = 0;
$(document.body).on('click', ".popCategory", function () {
var appendcategory = "";
if (counter < 100) {
counter++;
appendcategory += '<input type="text" class="form-control" name="category[]" placeholder="Category" value="545454" />';
$('.category_append').append(appendcategory);
}
});
</script>
As shown in image I have appended the category with the help of jquery and when I submit the form I get the result as shown in below:
The problem is that I only get one value when I have submitted three value of category . you can see the inspect element below:
<form action="" role="form" method="POST" enctype="multipart/form-data">
<input type="number" class="form-control" name="current_amount" placeholder="Amount" id="current_amount" min="1"/></td>
<td><select name="cash_flow_status" class="form-control">
<option value="cashin">CashIn</option>
<option value="cashout">CashOut</option>
</select></td>
<td class="category_append">
<input type="text" class="form-control" name="category[]" placeholder="Category" value="545454"/>
</td>
<td>
<a href="javascript:void(0)" class="popCategory">
<i class="fa fa-plus"></i></a>
</td>
<td class="item_append"><input type="text" class="form-control" name="items[]" placeholder="Items"/></td>
<td>
<div name="add1" class="btn btn-success cashFlowAddPart" onClick="appendItems()" ><i class="fa fa-plus"></i></div></td>
<td colspan="2">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" name="submit" id="cashflowsubmit" value="Submit" class="btn btn-success"/>
</td>
</form>
Can someone help me?

Categories