...

View Full Version : Form trouble



MHaris
08-19-2007, 02:23 PM
<?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?

MHaris
08-19-2007, 04:36 PM
Help?

CFMaBiSmAd
08-19-2007, 04:58 PM
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.

JBLC
08-19-2007, 10:38 PM
If $edit_cID is posted from another form, could you not use $_GET to retrive the variable from within the URL?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum