Viewed   83 times

Hello friends i have a trouble.

I'm trying to use this code to get dynamically, variables and values of a form, but there is a countless number checkbox, which may or not marked, I would like to know how can I get an off or "0" in case not this a checkbox labeled, these data have been used .ajax and data:

Short Example of checkboxes:

<input name="p-sis-0110-1" type="checkbox">
<input name="p-sis-0110-2" type="checkbox">
<input name="p-sis-0110-3" type="checkbox">
<input name="p-sis-0110-4" type="checkbox">
<input name="p-sis-0110-5" type="checkbox">
<input name="p-sis-0110-6" type="checkbox">

or

<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">

Ajax:

.$("#formarea").serialize()

PHP:

foreach ($_POST as $key => $value){
    echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>"
}

I appreciate any help to solve this little dilemma.

 Answers

2

Only "successful" controls are submitted. An unchecked checkbox or radio button is not "successful".

You need to declare a default value with a hidden input. Make sure the hidden input comes before the checkbox so if the checkbox is checked it will override the default hidden input since the names are the same:

<input name="p-sis-0110-1" type="hidden" value="0">
<input name="p-sis-0110-1" type="checkbox" value="1">

To use an array you need to explicitly define the indexes so they are the same:

<input name="input[0]" type="hidden" value="0">
<input name="input[0]" type="checkbox" value="1">
Saturday, November 12, 2022
 
2
foreach ($_POST as $key => $value)
    $message .= "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";

mail('liam@site.co.uk', 'sghting', $message);

$message = foreach ($_POST as $key => $value) is not correct, this will iterate over the results and store the last one. You want to store the values in your $message variable, not echo them.

Thursday, December 15, 2022
 
kato
 
5

You can use $.trim() to remove extra white-space in a string.

The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.

Source: http://api.jquery.com/jquery.trim

Ex.

function mysubmit{
    var contentbox      = $("#contentbox").html(),
        contentboxvalue = "contentboxvalue ='" + escape($.trim(contentbox)) + "'";
    $.ajax({
        type    :"POST",
        url     : "<?php echo base_url() ?>admin/data",
        data    : contentboxvalue,
        cache   : true,
        success : function() {
            document.getElementById("contentboxInfo").innerHTML = contentbox;
        }
    });
}
Saturday, October 29, 2022
 
caesay
 
3

Why don't you try constructing your data like this

var postData = {};
$('#items tr').not(':first').each(function(index, value) {
    var keyPrefix = 'data[' + index + ']';
    postData[keyPrefix + '[supp_short_code]'] = $(this).closest('tr').find('.supp_short_code').text();
    postData[keyPrefix + '[project_ref]'] = $(this).closest('tr').find('.project_ref').text();
    // and so on
});

Then in your AJAX call

data: postData,

Now your PHP script can process the data as a multi-dimensional array

<?php
if (isset($_POST['data']) && is_array($_POST['data'])) {
    foreach ($_POST['data'] as $row => $data) {
        echo $data['supp_short_code'];
        echo $data['project_ref'];
        // and so on
    }
}
Saturday, December 17, 2022
 
myitcv
 
21

mod_extract_forwarded is most supported, stable and available module for this. Included in all major distributions.

http://www.openinfo.co.uk/apache/

Friday, October 7, 2022
 
tar
 
tar
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :