DevelopPHP.com 2010 Discussion Archive(not active)

 

Forum Index >> JavaScript Forum

jQuery PHP checkbox values to PHP $_POST variables
   THREAD CLOSED
jQuery PHP checkbox values to PHP $_POST variables
by madpotatokipp - Sun Jan 09, 2011 11:05:02 PM
I am having an awful time figuring this out. I am using jQuery to get to values of all the checked checkboxes and store them into a jQuery Array. I am then using jQuery to post the array variable to my php parsing script. How can I get those values out of the $_POST? My script is posted below:

jQuery------------------

$("#delete_messages").click(function() {
var selected = new Array();
$("input:checkbox[name=mid]:checked").each(function() {
selected.push($(this).val());
});
if (selected != "") {
var answer = confirm("Do you really want to delete this message?");
if (answer) {
$.post("../scripts/remove_message_parse.php", {
mids: selected
}, function(data) {
alert(data);
});
}
}
});

PHP parse---------------

<?php
$redirect = basename($_SERVER['HTTP_REFERER']);
$redirect = explode("?", $redirect);
if ($redirect[0] == "user_inbox.php") {
include_once("../includes/phpfunctions.php");
foreach ($_POST as $key => $value) {
$$key = cleanpost($value);
}
$ids = explode(",", $mids);
$sql = "UPDATE user_messages SET to_user_delete='1' WHERE ";
foreach ($ids as $key => $value) {
$sql .= "id='".$value."' AND ";
}
$sql = substr($sql, 0, (strlen($sql)-4));
$res = mysql_query($sql);
if ($res) {
echo "Success. ".$sql;
} else {
echo "Failed.";
}
}
?>

I keep getting  "id='' " in my sql statement so nothing gets updated. Any thoughts?

RE: jQuery PHP checkbox values to PHP $_POST variables
by broli00 - Tue Jan 11, 2011 09:29:41 AM
Hi, I don't understand quite a few things here:

<?php
......
include_once("../includes/phpfunctions.php");
foreach ($_POST as $key => $value) {
$$key = cleanpost($value);
}
$ids = explode(",", $mids);
$sql = "UPDATE user_messages SET to_user_delete='1' WHERE ";
foreach ($ids as $key => $value) {
$sql .= "id='".$value."' AND ";
}
$sql = substr($sql, 0, (strlen($sql)-4));
$res = mysql_query($sql);
.....
?>

You could have also received the posted array by simply using:

$mids = $_POST['mids'];

What you have done is perfectly correct too though (useful when you have a lot of form elements) but I feel the above is easier.

Once you have the array ($mids), why are you exploding it with "," ??

You cannot explode an array (You can implode it and create a string), you can only explode a string into an array. And if you want to access the individual elements of the array:

foreach ($mids as $id) {
...
}

would have worked fine for this script.

May be I'm not fully understanding what's going on here so please correct me..

RE: jQuery PHP checkbox values to PHP $_POST variables
by broli00 - Tue Jan 11, 2011 09:34:53 AM
I actually wrote a similar code to test what you are trying to do here:

test.html
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script type = "text/javascript">
$(document).ready(function () {
    $('#submitBtn').click (function() {
        var selected = new Array();
$("input:checkbox[name=vehicle]:checked").each(function() {
selected.push($(this).val());
});
var selectedString = selected.join(",");
        $.post("test.php", {selected: selected },
        function(data){
            $('.result').html(data);
        });  
    });
});
</script>

<form name = "someForm" method = "post" action = "#">
    <input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
    <input type="checkbox" name="vehicle" value="Car" /> I have a car<br/>
    <input type="checkbox" name="vehicle" value="Aeroplane" /> I have an aeroplane<br />
    <input type="checkbox" name="vehicle" value="Ship" /> I have a ship<br/> 
    <a href = "#" id = "submitBtn">Submit</a>
</form>

<div class = "result">

</div>

test.php

<?php
$selected  = $_POST['selected'];

foreach ($selected as $value) {
    echo $value . " <br/>";

}
?>


^^^ Works perfectly fine for me. I think you need to get rid of the explode.



RE: jQuery PHP checkbox values to PHP $_POST variables
by toxin - Sat Feb 12, 2011 06:58:30 PM
Hi

I've a problem, cause I try to apply jQuery and Ajax comment system in to my site, and when I  submit my form and try to return the data in to a div, it returns nothing, just white page and in mysql there are only id numbers of this "messages", but the message field is empty.

Is there someone who can help me with this? Here is my scripts 

PHP:

<?php
$selected  = $_POST['selected'];

if(!empty($_POST['selected'])) {
echo $_POST['selected'];
}
else {
echo "empty";
}
?>




HTML/jQuery:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dokument bez tytułu</title>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script type = "text/javascript">
$(document).ready(function () {
    $('#submitBtn').click (function() {

var selected = $("textarea[name=selected]")
         $.post("test.php", {selected: selected },
        function(data){
            $('.result').html(data);
        });  
    });
});
</script>


<body>

<form name = "someForm" method = "post">
   <textarea name="selected" rows="5" cols="45" id="msgc"></textarea><br />

    <a href = "#" id ="submitBtn">Submit</a>
</form>

<div class ="result">

</div>
</head>
</body>
</html>


Sorry for my bad english, Please help if someone know how to Fix this :)

   THREAD CLOSED  
©2009 DevelopPHP.com