I have checkboxes of categories in a form. When checkbox is changed, from is submitted through ajax and get the values in PHP.
//form
foreach($somethings and $something){
<input class="input_sale_category" type="checkbox" name="category" value="something->value" />`
}
I get the form data and submit through ajax
$('.input_sale_category').click(function() {
var formData = $('#filter_sale_form').serialize();
jQuery.ajax({
type: 'GET',
url: myurl,
data: formData
success: function(response) {
console.log(response);
},
error: function (request, status, error) {
alert(request.responseText);
}
});
});
In PHP, I am getting input fields as a string
category=nexus-chair-offer&category=office-desks&category=office-storage
I tried to get the inputs values of category using explode, parse_str but could not get all the values of category
parse_str($str, $output);
var_dump($output);
How can I achieve this? Regex seems an option, but I not good at regex.
Issue is in you html code,
You are using same 'name' property in all of you input tag
What this will do is only sand 1 value to backend.
change you input tag like this
foreach($somethings and $something){
<input class="input_sale_category" type="checkbox" name="category[]" value="something->value" />`
}
check this answer as well, it might help,
Get checkbox values using checkbox name using jquery
You can also get values manually and pass it in ajax request's data
var checkboxes_value = [];
$('.input_sale_category').each(function(){
//if($(this).is(":checked")) {
if(this.checked) {
checkboxes_value.push($(this).val());
}
});
//checkboxes_value will be array of checked checkboxes
Related
I am working on code igniter. i have one dynamic form where i want to
populate some data with the help of one dropdown list. I want to
display that data into input box rather than another option value. I
got one online code that populate data with ajax call into another
select box its working for me. but if i used input box then its not
working for me.
My ajax call :
var baseURL= "<?php echo base_url();?>";
$(document).ready(function()
{
$('#employee').change(function(){
var EMPLOYEE_ID = $(this).val();
// AJAX request
$.ajax({
url:'<?php echo base_url()?>/Responsible/getEmpData',
method: 'post',
data: {EMPLOYEE_ID :EMPLOYEE_ID },
dataType: 'json',
success: function(response){
alert(response);
// Remove options
$('#sel_depart').find('input').not(':first').remove();
$.each(response,function(index,data){
//$('#sel_depart').append('<option value="'+data['DETAIL_ID']+'">'+data['NI_NUMBER']+'</option>');
$('#sel_depart').append('<input type="text" class="span8" name="ni_number" value="'+data['NI_NUMBER']+'"/>');
});
}
});
});
});
As you can view my commented code of option value is working.I am trying to same thing on input box but my $('#sel_depart').find('input').not(':first').remove(); this line is not working.
My input box:
<input id='sel_depart' type="text" class="span8" name='ni_number' value=""/>
My controller :
public function getEmpData()
{
$postData = $this->input->post();
$this->load->model('Pay_slips_model');
$data = $this->pay_slips->getEmpData($postData);
echo json_encode($data);
}
Model :
public function getEmpData($postData)
{
$response = array();
$this->db->select('ed.DETAIL_ID,ed.NI_NUMBER');
$this->db->where('EMPLOYEE_ID', $postData['EMPLOYEE_ID']);
$q = $this->db->get('employee_details ed');
$response = $q->result_array();
return $response;
}
I am getting duplicate or multiple NI_NUMBER for each EMPLOYEE_ID.In my table employee_details I am having only one NI_NUMBER for each EMPLOYEE_ID. If i inspect my input box after ajax call i can see duplicate data in input box but its not showing on form.What is is the solution for both the problems.
Not able to display data in input box rather than select box.
how to prevent from duplicate rows from table.
I'm developing a PHP+jQuery application. I'm quite noob with PHP.
Anyway, I'm trying to send a serialized form to a PHP page that will store data to session, via jQuery.
This is a dynamic form, where I could have many input like this:
<input type="text" name="name[]" />
And this is an example of my serialized form:
name[]=name1&name[]=name2
I've tried to get the array with $_POST["name"] from the PHP page, but it did not work.
How can I get this array from the PHP page?
Are you doing something like this???
$(function() {
$('.submit').click(function() {
var names = $('input[name="name[]"]').map(function(){
return this.value;
}).get();
$.ajax({
type: 'POST',
url: url,//this should link to your page
data: {
'name[]': names,
// other data
},
success: function() {
}
});
});
});
</script>
I have a form with several identical fields:
<input type="text" id="qte" value="" name="qte[]">
How transmetre the array in my file processing?
I noticed that the array sent ajax became a string.
$("#form_commande").submit(function(event){
var qte = $("#qte").val();
if(qte== '')
{
$('#qte_message').html("KO QTE");
}
else
{
$.ajax({
type : "POST",
url: $(this).attr('action'),
data: $(this).serialize(),
success : function(){
$('#form_commande').html('<p>OK</p>');
},
error: function(){
$('#form_commande').html("<p>KO</p>");
}
});
}
return false;
}
Get value in jquery like:
$("#form_commande").submit(function(event){
var qte_array = new Array();
$('input[name="qte[]"]').each(function(){
qte_array.push($(this).val());
});
if(qte_array.length== 0)
{
$('#qte_message').html("KO QTE");
}
else
{
$.ajax({
type : "POST",
url: $(this).attr('action'),
data: {qte:qte_array},
success : function(){
$('#form_commande').html('<p>OK</p>');
},
error: function(){
$('#form_commande').html("<p>KO</p>");
}
});
}
});
and get it in php like:
$qte = $_POST["qte"];
here qte an array
This returns the input textbox object:
$("#qte");
This returns the value of the input textbox object:
$("#qte").val();
Remember you asked for DOM object by id, and this by definition returns only one.
Please read this topic:
JQuery - Reading an array of form values and displaying it?
In short you should iterate with tag name="qte[]" instead of using ids. In DOM you cannot have two different objects with different ids.
var qte_array = new Array();
$('input[name="qte[]"]').each(function(){
qte_array.push($(this).val());
});
After this you have all the values of qte[] array in one object - qte_array. You can later serialize this array to JSON and then pass it as a string.
ALTHOUGH - you shouldn't need to do all those things. You can send ajax request directly with your form, all those data in these inputs will be transferred anyway. You just need to handle them correctly server-side.
id is unique, you can't have more fields with the same ID.
By the way you should convert values to JSON and pass them to Ajax
JQUERY:
$(document).ready(function(){
$('form').submit(function(){
var content = $(this).serialize();
//alert(content);
$.ajax({
type: 'POST',
dataType: 'json',
url: 'http://localhost/test/generate',
timeout: 15000,
data:{ content: content },
success: function(data){
$('.box').html(data).fadeIn(1000);
},
error: function(){
$('.box').html('error').fadeIn(1000);
}
});
return false;
});
});
HTML:
<form>
<input type="checkbox" value="first" name="opts[]">
<input type="checkbox" value="second" name="opts[]">
<input type="checkbox" value="third" name="opts[]">
<input type="submit">
</form>
How do i process (or read) multiple checked checkbox's value in PHP? I tried doing $_POST['content'] to grab the serialized data but no luck.
Replace:
data:{ content: content } // <!-- you are prefixing with content which is wrong
with:
data: content
Now in your PHP script you can use $_POST['opts'] which normally should return an array.
Try
echo $_POST['opts'][0]. "<br />";
echo $_POST['opts'][1]. "<br />";
echo $_POST['opts'][2]. "<br />";
You post an array to the Server and it is available in the post variable 'opts'. Remember: Unchecked boxes dont get posted.
The chosen answer still didn't work for me, but here is what did:
var checkedBoxesArr = new Array();
$("input[name='checkedBoxes']:checked").each(function() {
checkedBoxesArr.push($(this).val());
});
var checkedBoxesStr = checkedBoxesArr.toString();
var dataString = $("#" + formID).serialize() +
'&checkedBoxesStr=' + checkedBoxesStr;
[The above code goes in your javascript, before serializing the form data]
First, cycle through the checked boxes and put them into an array.
Next, convert the array to a string.
Last, append them to the serialized form data manually - this way you can reference the string in your PHP alongside the rest of the serialized data.
This answer came partly from this post: Send multiple checkbox data to PHP via jQuery ajax()
there are an Error in your code :
The url should be url: 'http://localhost/test/generate.php' with the extension name
<input id="u1" class="username">
<input id="u2" class="username">
<input id="u3" class="username">
...
How to fetch input value with "username" class and send with ajax jquery to php page.
i want to recive data like simple array or simple json. (i need INPUT values and not ids)
var inputValues = [];
$('input.username').each(function() { inputValues.push($(this).val()); });
// Do whatever you want with the inputValues array
I find it best to use jQuery's built in serialize method. It sends the form data just like a normal for submit would. You simply give jQuery the id of your form and it takes care of the rest. You can even grab the forms action if you would like.
$.ajax({
url: "test.php",
type: "POST",
data: $("#your-form").serialize(),
success: function(data){
//alert response from server
alert(data);
}
});
var values = new Array();
$('.username').each(function(){
values.push( $(this).val());
});