|
Posted by noone on 11/18/23 11:43
walterbyrd@iname.com wrote:
> I am trying to get user input from a php form. Problem is: the form
> doesn't wait for the user to press the submit button, but falls right
> through. Therefore the variable I am trying to get, is never set. I
> know it has to be something simple, but I'm missing it. Here is the
> code:
> echo "<form action=\"$PHP_SELF\" method=\"post\">";
> echo "Name of new database to create: ";
> echo "<br><input type=\"text\" name=\"new_db\">";
> echo "<input type=\"submit\" value=\"submit\"><br><br>";
> if ($new_db)
> {
> $create_db = 'CREATE DATABASE '.$new_db;
> if (mysql_query($create_db, $conn))
> {
> echo "Database $new_db created successfully\n";
> }
> else
> {
> echo 'Error creating database: ' . mysql_error() . "\n";
> }
> }
> else
> {
> echo ("new database not created");
> };
You need something that stops at the form - I have used either a _SESSION
variable or a hidden variable.
like this:
<?php
if (isset($_POST['hiddenvar']))
// hidden variable set - process input form
{
if ($new_db)
{
$create_db = 'CREATE DATABASE '.$new_db;
if (mysql_query($create_db, $conn))
{
echo "Database $new_db created successfully\n";
}
else
{
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
else
{
echo ("new database not created");
};
}
// hidden variable not set - display form
else {
echo "<form action=\"$PHP_SELF\" method=\"post\">";
echo "Name of new database to create: ";
echo "<br><input type=\"text\" name=\"new_db\">";
echo "<input type=hidden value=1 name=hiddenvar>";
echo "<input type=\"submit\" value=\"submit\"><br><br>";
}
?>
Navigation:
[Reply to this message]
|