Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Form trouble

  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Form trouble

    PHP Code:
    <?php

    // Last Modified 18th August 2007

    ob_start();
    session_start();

    include(
    'templates/header.php'); // Header template
    include('../config/config.php'); // Includes configuration files
    include('../lib/functions.php'); // Load all functions from Library

    if(!session_is_registered("username")){
    header('Location: index.php');
    exit();
    }
    else {

    $submit_delete $_POST['delete'];
    if(isset(
    $submit_delete)){

    $sql mysql_query('SELECT * FROM candidates ORDER BY id') or die(mysql_error());

    $selected_candidate $_POST['selected_candidate_array'];
    foreach (
    $selected_candidate as $cID){
        
    mysql_query("DELETE FROM candidates WHERE id='$cID'") or die(mysql_error());
        
    mysql_query("DELETE FROM answers WHERE candidate_id='$cID'") or die(mysql_error());
        echo 
    "Candidate <b>".$cID."</b> deleted!<br/>";
    }

    }

    else {
        echo 
    "<form method=\"POST\">";
        echo 
    "Enter new name: <input type=\"text\" name=\"new_name\">";
        echo 
    "<input type=\"submit\" name=\"submit_new_name\" value=\"Edit\">";
        echo 
    "</form>";
        
    $submit_new_name $_POST['submit_new_name'];
        
    $new_name sql_safe($_POST['new_name']);
        
    $edit_cID $_POST['selected_edit_candidate'];
        echo 
    $edit_cID;
        if(isset(
    $submit_new_name)){
            echo 
    $edit_cID;
            
    mysql_query("UPDATE candidates SET name='$new_name' WHERE id='$edit_cID'") or die(mysql_error());
            echo 
    $new_name;
            echo 
    "Name successfully changed to ".$new_name;
        }
    }
    include(
    'templates/footer.php'); // Footer Template

    }

    ?>
    $edit_cID is posted from another form. The $edit_cID echo's out corectly before if(isset($submit_new_name)) but not after it has been submitted.

    Why and how can I fix it?

  • #2
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Help?

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    Web servers and web pages are "stateless." They don't know and don't care what happened before nor what will happen next. A normal variable (including $_POST variables) only exists for the duration of the execution of the script on the page they were set on. When a script reaches the end of execution, all resources it used are destroyed. Unless you specifically propagate a variable between pages (submitting a form, even to the same URL, is a separate HTTP request for that page) in a session variable/hidden form field/parameter on the end of the url..., that variable does not exits past the end of execution on the page it was set on.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    New Coder
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    If $edit_cID is posted from another form, could you not use $_GET to retrive the variable from within the URL?


  •  

    Posting Permissions

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