简体   繁体   中英

Post not being recieved from checkbox form php

I'm posting from the HTML code shown in this jsfiddle to the PHP page for which the code is below. The issue is that the array $_POST['selectedpost'] isn't being received. That's the array containing which checkboxes were ticked. In the js fiddle I added in an example row to the table containing the checkboxes as normally these are generated using PHP and SQL.

<?php
include "connect2.php";

if (isset($_POST['selectedpost'])) {
$postschecked = $_POST['selectedpost'];
$length = count($postschecked);
}
else{
returnpage();
}

if (isset($_POST['deleteposts'])) {
foreach($postschecked as $post_id){
    $sql = "DELETE FROM posts WHERE post_id='$post_id'";
    mysql_query($sql);
}
returnpage();
}

if (isset($_POST['passposts'])) {
foreach($postschecked as $post_id){
    $sql = "UPDATE posts SET moderation=1 WHERE post_id='$post_id'";
    mysql_query($sql);
}
returnpage();
}

if (isset($_POST['editpost'])) {
if ($lenght==1){
    foreach($postschecked as $post_id){
        header("location:editpost.php?post_id=$post_id");
    }
}
else{
    returnpage();
}
}

if (isset($_POST['returnpost'])) {
if (isset($_POST['reasonreturned'])) {

    foreach($postschecked as $post_id){

        $sql = "SELECT description FROM posts WHERE post_id='$post_id'";
        $query = mysql_query($sql);
        $array = array();
        while ($row = mysql_fetch_array($query, MYSQL_NUM)) {
        $array[] = $row; }
        $description = "".$array[0][0];

        $description = $description . "<br/><br/><span style='color:red;font-size:18px;'>" . $_POST['reasonreturned'] . "</span>";

        $sql = "UPDATE posts SET description='$description' WHERE post_id='$post_id'";
        $query = mysql_query($sql);
    }
}
foreach($postschecked as $post_id){
    $sql = "UPDATE posts SET moderation=3 WHERE post_id='$post_id'";
    $query = mysql_query($sql);
}
returnpage();
}

if ($length){
returnpage();
}

function returnpage(){
//header("location:moderate.php");
}
?>

http://jsfiddle.net/3A6az/2/

Also extra note, I am aware as to how un-efficient my code is in places and I'm also aware to the fact I should drop mysql and move to something like mysqli. Thank's for any help given

If you have more than 1 checkbox you need to use

name='selectedpost[]'

It will then be available to you with $_POST['selectedpost']; as an array.

Hope this helps!

You're using an unbracketed input <input type='checkbox' name='selectedpost' value='404'></input> plus you don't need </input> <(FYI)

If anything you shouldn't be using value='404' unless that's what you want to pass as a "value".

You probably meant to use multiple checkboxes and using name='selectedpost[]'

Ie:

<input type='checkbox' name='selectedpost[]'>

Using square brackets [] are treated as an array.


Footnotes:

I would like to point out though, that switching to mysqli_* functions would be most beneficial. mysql_* functions are deprecated.

Using mysqli_* functions with prepared statements or PDO would be even better in order to protect yourself from SQL injection.

Here is a guide on how to prevent SQL injection: How can I prevent SQL injection in PHP?


NB: I also found a typo which may give you trouble if ($lenght==1){

You have the word $length in your code as well. Change it to if ($length==1){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM