Jump to content

[SOLVED] $_POST checkbox


Ken2k7

Recommended Posts

I'm trying to create a validate members page that lists all the unvalidated members and beside it is a checkbox with the name attribute being the member's name. Is there a way to create a foreach loop to check all the $_POST and check if it's empty, and if not, do something? Thanks.

Link to comment
Share on other sites

you can use ANY loop, and if its its empty, the loop would then be false,right?

 

WHY not do this:

 

do a select in your database, where validation is != validated, do a mysql fetch, and add it into the loop, right? in the loop, type whatever u wish: username, and the checkbox, set the value of the checkbox = the persons name, and write the rest of the code, and blah blah

 

if theres no results, then do a simple, if mysql_fetch brings back 0 entries, then echo " blah lah" or w/e u want

Link to comment
Share on other sites

Give the checkbox a value of True, that way if it is checked, it will return true, otherwise it will return null, or 0, which is false.

Really? How does that work? It returns "on." And that's the problem. I can't tell which is checked if it just returns on.

Link to comment
Share on other sites

R u want to delete that users or want to active that users.

 

If i get u correctly do this:

Use count function if greater than 0 then do a query if not give error msg.

Right but see I use a foreach loop:

foreach ($_POST as $p)

 

but the problem is that $_POST['submit'] counts. :( Is there anyway to exclude that in the foreach loop?

Link to comment
Share on other sites

This is my test code that I used:

 

if($_REQUEST['act'] == "check") {
 echo $_POST['test'];
 } else {
 echo "<form action=\"{$_SERVER['PHP_SELF']}?act=check\" method=\"POST\">";
 echo "<input type=\"checkbox\" value=\"true\" name=\"test\">";
 echo "<input type=\"submit\" value=\"Check\">";
 echo "</form>";
 }

 

This prints a checkbox with a value of true and a submit button to the page, when clicked, it prints the POST array value of the checkbox, named "test", when I check the box and click the button, it prints "true" to the page, when I uncheck the box and click the button, nothing is printed.  Use this example to try to figure out the answer.  Nothing is returned when the checkbox is not checked, so empty() would be true, meaning that you could do this:

 

if(!empty($_POST['checkbox'])) { // Checkbox is checked, value is returned
 // Do code for "true"
 } else { // Checkbox is not checked.
 // Do code for "false"
 }

Link to comment
Share on other sites

Try it like this:

this will be in ur while loop for fetching records:

echo "<td align='center'><input type='checkbox' id='del[]' name='del[]' value='$row->userid'></td>";

 

Now try this:

 

if (isset($_POST['delete'])){
    if(count($_POST['del']) > 0) {
        foreach ($_POST['del'] as $id) {

            $sql = "delete from table WHERE userid='$id'";             
            $query=mysql_query($sql) or die (mysql_error());
            if ($query){//this is for 2nd table if have..
        $sql1 = "delete from 2nd_table WHERE userid='$id'";             
            $query1=mysql_query($sql1);               
        }
        }
        header('location:Data_is_deleted.php');
        exit;
    	

     }else {
         echo ("No Checkboxes are selected!");
    }
}

 

Hope this will help.

Link to comment
Share on other sites

I am having a similar problem with my code. rather than post a new thread I figure since we are already on the subject ya'll could look at it. All of my foreach loops (there are 8 of them) were I am receiving the arrays I get this error. Warning: Invalid argument supplied for foreach()  The only problem is I can't figure out why or where.

 

This page has 3 passes. The first is a form. The second receives the form and the 3 isn't even developed yet because I can't get passed this.

 

well here is the code

 

 

 

 

 

<?php @session_start(); ?>

<?php

//get userid needed ad so on

$loginID=$_SESSION['loginID'];

$username=$_SESSION['username'];

 

//checks to see if the user is logged in

if(!isset($_SESSION['username'])){

die ("You either are not logged in or you do not have permission to view this page you must log in. If you feel this is a mistake click here to <a href='../logout.php'>log out</a> and then immediatly log back in and try agian.");

}

 

include("../constents.php");

 

 

 

 

 

if(!isset($_POST['post']))

{

mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql' . mysql_error());

 

//search the database to see properties that need to be paid for

$sql="SELECT * FROM ".$dbname.".tbl_props WHERE LoginID='".$loginID."' and userdeactive='0'";

$result=mysql_query($sql);

$numberProps=mysql_num_rows($result);

 

//This area will hold the logic and stuff determining price of sale and whitch properties will be paid for this will post to the paypal secured area. remember https:

 

?>

 

 

<p>You have <?=$numberProps ?> properties in our system.</p>

 

    <p>When selecting the lenght of time you wish to pay for a property you can select multiple lengths and those lenghts will add together. So say you want to pay for a property for 4 months you can do this by selecting the 3 month check box and the 1 month check box.</p>

    <p>To delete a property out of the system go into manage your listings and select delete proerty for the property you wish to delete.

    <form method="post" action="<?=$_SERVER['PHP_SELF'] ?>">

    <input name="post" type="hidden" value="1" />

<table>

    <tr>

    <td><p>Title - Number</p></td><td>When Listed</td><td>Paid Till</td><td>Pay for</tr>

<?php //this while statment echos out the property name num date listed date paid for.

$pay=1;

 

while( $row = mysql_fetch_assoc($result)){

 

 

?>

    <tr><td>

   

<p><?=$row['title'] ?> - <?=$row['propID'] ?> </p></td>

    <td><p><?=$row['whenlisted'] ?></td>

<td><p><?=$row['paidTill'] ?> </p></td>

    <td>1 Month<input name="onemonth[]" type="checkbox" value="<?=$row['propID'] ?>" />

    3 Month<input name="threemonth[]" type="checkbox" value="<?=$row['propID'] ?>" />

    6 month<input name="sixmonth[]" type="checkbox" value="<?=$row['propID'] ?>" />

    year<input name="year[]" type="checkbox" value="<?=$row['propID'] ?>" />

 

    </td>

 

<?php

$pay++;

$storedprops=$storedprops+" & "+$row['propID'];

 

 

}

echo "</tr>";

echo "</table>";

echo "<input name='submit' type='submit' value='Submit' />";

echo "</form>";

   

   

} elseif($_POST['post']==1) {

//this is the cycle back.

//recive the post

$onemonth=$_POST['onemonth'];

$threemonth=$_POST['threemonth'];

$sixmonth=$_POST['sixmonth'];

$year=$_POST['year'];

 

//recieve the arrays from the check boxes

$total1month=count($onemonth);

$total3month=count($threemonth);

$total6month=count($sixmonth);

$totalyear=count($year);

 

 

$total_props_purch=$total1month+$total3month+$total6month+$totalyear;

 

//find out price structure depending on total props paid for

if($total_props_purch==1) {

$onemonthprice=14.95;

$threemonthprice=41.95;

$sixmonthprice=77.95;

$yearprice=9.95;

$qualify="You qualify for no discounts";

 

} elseif($total_props_purch>=2) {

$onemonthprice= 11.48;

$threemonthprice= 31.48;

$sixmonthprice= 58.48;

$yearprice= 74.98;

$qualify="You qualified for the 2 property discount";

 

} elseif($total_props_purch>=4) {

$onemonthprice= 9.49;

$threemonthprice= 26.24;

$sixmonthprice= 38.99;

$yearprice= 50;

$qualify="You qualified for the 4 or more property discount.";

}

 

//now that you have the prices add them up

$a1=$total1month*$onemonthprice;

$a2=$total3month*$threemonthprice;

$a3=$total6month*$sixmonthprice;

$a4=$totalyear*$yearprice;

$totalprice=$a1+$a2+$a3+$a4;

 

//figure out the property ID's that are being paid for for each section

$id=0;

$a1mi=0;

$a3mi=0;

$a6mi=0;

$ymi=0;

 

foreach($onemonth as $value){

$property_id[$id]=$value;

$onemonthselected=$value;

$a1mi++;

$id++;

}

 

foreach($threemonth as $value){

$property_id[$id]=$value;

$threemonthselected=$value;

$a3mi++;

$id++;

}

 

foreach($sixmonth as $value){

$property_id[$id]=$value;

$sixmonthselected=$value;

$a6mi++;

$id++;

}

 

foreach($year as $value){

$property_id[$id]=$value;

$yearselected=$value;

$ymi++;

$id++;

}

 

echo "Your total for the following properties is $".$totalprice;

echo $qualify;

 

echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";

echo "<input name='post' type='hidden' value='2' />";

 

 

echo "<h1>1 Month Payment</h1>";

foreach($onemonthselected as $value){

echo $value."<br />";

}

 

echo "$ ".$onemonthprice." each";

 

 

echo "<h1>3 Month Payment</h1>";

foreach($threemonthselected as $value){

echo $value."<br />";

}

 

echo "$ ".$threemonthprice." each";

 

echo "<h1>6 Month Payment</h1>";

foreach($sixmonthselected as $value){

echo $value."<br />";

}

 

echo "$ ".$sixmonthprice." each";

 

 

echo "<h1>Full Year Payment</h1>";

foreach($yearselected as $value){

echo $value."<br />";

}

 

echo "$ ".$yearprice." each";

 

$i=0;

while($i <= $total_props_purch){

?>

<input type="hidden" name="$property_id[]" value="<?=$property_id[$i] ?>" />

   

    <?

$i++;

}

 

echo "To remove these properties or change the length of time <a href='".$_SERVER['PHP_SELF']."'>please follow this link</a> or click the back button <br />";

?>

    <input type="hidden" name="ammount" value="<?=$totalprice ?>" />

 

<?

echo "<input name='Next Step' type='submit' />";

 

 

 

} elseif($_POST['post']==2){

 

echo "post 2 congradulations I can move on to the input part";

 

 

}

 

?>

 

Link to comment
Share on other sites

Ok, first off: Hijacking another person's thread = WRONG.  Don't do it, start your own, link to this thread and say that you are having a similar problem.

 

Second: use code tags, I cannot stand scrollong all the way down (ignoring the unformatted code) to hit reply to reprimand a person for not using code tags.  USE THEM!

Link to comment
Share on other sites

Sorry I'm still new to this whole form thing. But I figured out my problem while phpfreaks was down over the last few days I built my arrays wrong. I had a problem with an incrementing variable that throw my array off. My apologies again to you for "Stealing the Thread" Just thought if I had a similar problem it would be helpful to both parties. Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.