473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

super easy questions

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");
};

Mar 27 '06 #1
2 1484
wa********@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>";

}
?>
Mar 27 '06 #2
You have it structured as if you expect it to wait for the form the
first time through. Remember, the first time the page is displayed
there is no input, so ALL the php ont the page will be executed, but
there will be no data for it to work on. Only AFTER the user clicks the
submit button and THE PAGE IS RELOADED will the data be available.

Most often, code to process the data is put before the table itself
since you will need to get that data THE SECOND TIME THROUGH to make
the form entries sticky.

For example:

<?
if (isset($_POST['submitted'])) {
$input_value=$_POST['stuff'];
}
?>
<FORM ACTION="thispage.php" METHOD='post'>
<INPUT TYPE='text' name="stuff" VALUE="<? echo $input_value;?>">
<INPUT TYPE="hidden" NAME="submitted" VALUE="TRUE">
<INPUT TYPE="submit" VALUE="DO IT">
</FORM>

Notice that by picking the value out of the $_POST array before you get
to the FORM tage then the value that was entered in the form THE FIRST
TIME THROUGH, gets stuck in as the default value in the form THE SECOND
TIME THROUGH.

--gary

Mar 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John | last post by:
Hi, I'm looking for the best way to deal with globals in PHP. As a 'C' software developer, I would normally avoid all globals and not have any at all, but use structs and pass everything in...
2
by: Clarence Gardner | last post by:
The super object is considered a solution to the "diamond problem". However, it generally requires that the ultimate base class know that it is last in the method resolution order, and hence it...
11
by: Nicolas Lehuen | last post by:
Hi, I hope this is not a FAQ, but I have trouble understanding the behaviour of the super() built-in function. I've read the excellent book 'Python in a Nutshell' which explains this built-in...
10
by: Chris Green | last post by:
Good day, I've done a bit of searching in the language reference and a couple pages referring the behavior of super() but I can't find any discussion of why super needs the name of the class as...
6
by: David Hirschfield | last post by:
I'm having trouble with the new descriptor-based mechanisms like super() and property() stemming, most likely, from my lack of knowledge about how they work. Here's an example that's giving me...
9
by: Mike Krell | last post by:
I'm reading Alex Martelli's "Nutshell" second edition. In the section called "Cooperative superclass method calling", he presents a diamond inheritance hierachy: class A(object): def...
4
by: Noah | last post by:
Am I the only one that finds the super function to be confusing? I have a base class that inherits from object. In other words new style class: class foo (object): def __init__ (self, arg_A,...
0
leafgreen
by: leafgreen | last post by:
Hey everyone. Thanks for helping me with these two super-simple newbie questions. I use DW8. 1. On this page http://adswebsite.net/yahoo-search-versus-google-adwords, on the right column where it...
1
by: cjdewitt | last post by:
First I would like to thank Albert Kallal for the Super Easy Word Merge. I have been using this in an Access 2003 database for a while, then upgraded to Access 2007 - worked until I split the...
25
by: dylan m. austin | last post by:
Hello list. This is my first message and it's nothing needful but rather playful. I'd just like to hear some thoughts on something I consider to be a curiosity of javascript usage. I've seen a lot...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.