SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sending and receiving PHP arrays using HTML form

    Hi there
    I am trying to send an array of items to delete from a form, receive them back to the page then enter them into another form and then send them back to the page. Basically its a delete then confirm delete thing.
    So I can send the array from the first form though I am not sure how to place them into the second (confirm delete) form.
    What I've got so far considering the array does get sent in the first place is
    First I receive the array:
    PHP Code:
    if (isset($_POST['delete']))
    {
        if (isset(
    $_POST['todelete'])){
        
    $todelete[] = ($_POST['todelete']);////here is my problem I think
    echo <<<_END
        <div>Are you sure you wish to make these changes</div>
        <div><form action="" method="post">
        <input type='hidden' name='todelete'value="
    $todelete"/>
        <input type='hidden' name='confirm'value="confirm"/>
        <input id='inputform' type='submit' size='50' value='Yes' />
        </form>
        <form action="" method="post">
        <input type='hidden' name='rollback'value='rollback' />
        <input id='inputform' type='submit' size='50' value='No' />
        </form></div>
    _END;

        }

    Then I try and receive the array back with
    PHP Code:
        if (isset($_POST['confirm'])){
            foreach (
    $_POST['todelete'] as $delete_id) {
                
    $query ="DELETE FROM gallery WHERE id = $delete_id";
                
    $result=mysql_query($query) or die("Invalid Query : ".mysql_error()); 
            echo 
    "Removing Data";
            }

        } 
    What I receive is a warning:
    'Warning: Invalid argument supplied for foreach() in C:\wamp\www\css\enterphotos.php on line 91"
    I am sure that there is a simple solution! Can anyone help me here?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
        if (isset($_POST['confirm'])){

            
    var_dump($_POST['todelete']);
            exit;

            foreach (
    $_POST['todelete'] as $delete_id) {

                
    $query ="DELETE FROM gallery WHERE id = $delete_id";

                
    $result=mysql_query($query) or die("Invalid Query : ".mysql_error()); 

            echo 
    "Removing Data";

            }



        } 
    The only code I hate more than my own is everyone else's.

  3. #3
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this oddz though it still didn't work. What I received was 'string(5) "Array" ' instead.

  4. #4
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks webgypsy
    With a little playing around I got this to work.
    You rock!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •