...

View Full Version : PHP Forms



MHaris
08-18-2007, 09:51 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 questions') or die(mysql_error());

$selected_question = $_POST['selected_question_array'];
foreach ($selected_question as $qID){
mysql_query("DELETE FROM questions WHERE id='$qID'") or die(mysql_error());
echo "Questions".$qID."deleted!<br/>";
}

}

$submit_edit = $_POST['edit'];
if(isset($submit_edit)){
echo "<form method=\"POST\">";
echo "Enter new question: <input type=\"text\" name=\"new_question\">";
echo "<input type=\"submit\" name=\"submit_new_question\" value=\"Edit\">";
echo "</form>";
$submit_new_question = $_POST['submit_new_question'];
$new_question = $_POST['new_question'];
if(isset($submit_new_question)){
echo $_POST['new_question'];
}
}

include('templates/footer.php'); // Footer Template

}

?>

Yup, the form under $submit_edit is not working.It is not echoing the question $_POST['new_question'];

The form is being processed from

<?php

// Last Modified 18th August 2007

ob_start();
session_start(); // Loads session

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 {

include('templates/questions.php'); // Add new question form

$submit = $_POST['submit'];
$question = sql_safe($_POST['question']);

if(isset($submit)){
mysql_query("INSERT INTO questions(question) VALUES('$question')") or die(mysql_error());

$newly_created_question = mysql_query("SELECT * FROM questions ORDER BY `id` desc LIMIT 1");
while ($row = mysql_fetch_array($newly_created_question)){
$qID = $row['id'];
}

$result = mysql_query("SELECT id FROM candidates");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$cID=$row[0];
$query = "INSERT INTO answers(answer,candidate_id,question_id) VALUES('1','$cID','$qID')";
mysql_query($query)or die(mysql_error());

}

}

$sql = mysql_query('SELECT * FROM questions') or die(mysql_error());
echo "<form method=\"post\" action=\"delete_edit_question.php\">";
while($row = mysql_fetch_array($sql)){
$qID = $row['id'];
echo "<b>Question #</b>".$row['id']." - ".$row['question']."?";
echo "<br/>Delete:<input type=\"checkbox\" name=\"selected_question_array[]\" value=\"".$qID."\"><br/>";
echo "Edit:<input type=\"radio\" name=\"selected_edit_question\" value=\"".$qID."\"><br/>";
echo "<br />";
}
echo "<input type=\"submit\" name=\"delete\" value=\"Delete\">";
echo "<input type=\"submit\" name=\"edit\" value=\"Edit\">";
echo "</form>";

mysql_close(); // Closes DB connection
}

include('templates/footer.php'); // Footer Template

?>

Inigoesdr
08-18-2007, 10:11 PM
$submit_edit/$_POST['edit'] is empty because there's nothing in the form called "edit". Use a hidden form variable or change $_POST['edit'] to $_POST['submit_new_question'] or whatever you want to use for that page.

MHaris
08-19-2007, 12:44 AM
$submit_edit/$_POST['edit'] is empty because there's nothing in the form called "edit". Use a hidden form variable or change $_POST['edit'] to $_POST['submit_new_question'] or whatever you want to use for that page.

There's a button.


echo "<input type=\"submit\" name=\"edit\" value=\"Edit\">";

The problem is with


$submit_new_question = $_POST['submit_new_question'];
$new_question = $_POST['new_question'];
if(isset($submit_new_question)){
echo $_POST['new_question'];
}

MHaris
08-19-2007, 02:08 AM
Any ideas?

Inigoesdr
08-19-2007, 02:19 AM
Since this form doesn't have an "action" it has to post to itself. The elements in the form are: new_question & submit_new_question. However, they aren't set until after the form is submitted, and since this form doesn't contain anything named "edit" for the first and second lines, it doesn't ever get to your echo.


$submit_edit = $_POST['edit'];
if(isset($submit_edit)){
echo "<form method=\"POST\">";
echo "Enter new question: <input type=\"text\" name=\"new_question\">";
echo "<input type=\"submit\" name=\"submit_new_question\" value=\"Edit\">";
echo "</form>";
$submit_new_question = $_POST['submit_new_question'];
$new_question = $_POST['new_question'];
if(isset($submit_new_question)){
echo $_POST['new_question'];
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum