By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

Call a local function or a separate PHP file?

P: n/a
Hello,

I want to write some code such that it'll update (ie insert, delete, etc)
some data to/from a mySQL database whenever a user hit an HTML form button
(ie Submit). Now what I find annoying is that this form can only, from my
understanding, execute a separate PHP file with the <form
action=removerow.php> code. Or can I call a function?

The reason being I find that the form will only pass a single variable to
this "removerow.php" whereas I have a few data that I want to pass onto a
function for execution. For example, if I want to display a table of
multiple rows of data queried from a database, with a check box next to each
row for the user to select for deleting from the database, I need to use an
array variable to store the list of rows that need to be removed and pass
this variable to "removerow.php". So I don't know how or if I can pass
other information stored in other variables as well to "removerow.php". Can
someone please clarify?

Also what I'm not sure if it is true or not. But it appears that whenever
PHP begins executing a different PHP file, all global variables that exist
from the previous PHP file are lost! Is this true? For example, all my
other variables information are lost when "removerow.php" executes.

Can I have it such that "removerow.php" don't need to be called and the PHP
script can call instead a local function to execute the job?

Thanks
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Titus Cheung wrote:
Can I have it such that "removerow.php" don't need to be called and the PHP
script can call instead a local function to execute the job?


No. PHP is an interpretor that executes as the web server (usually
Apache) is being asked for the page. Once the page has been given to the
user, PHP has done its job. To run more script, another page has to be
called for, and so on. That is server-side scripting.

You're thinking of client-side scripting (such as - spit, curse -
JavaScript). That is executed by the web browser (which is why it will
work for some users, and throw object errors for most others).

However, you're obviously new to server-side scripting, and you haven't
learned the tricks of the trade. Here are the basics.

A form calls another PHP page using the action='php-page.php' attribute
in the form tag. If you are going to do something 'unique' with the data
that you pass to php-page.php, such as update a database, which you will
only want to happen once for that data, then you should set method to
POST. So this gives you something like this:

<form action='php-page.php' method='post'>

If the data is just used to do something common, such as call a list of
results that people are going to want to be able to see over and over
again, then use method='get'. The GET method puts the data into the URL
itself, so the URL can be cached. POST does not allow caching, and the
data is passed as part of the HTTP request.

You also state that only one variable can be sent to a PHP page in a
form. This is not true at all. You can send as much as you like (in a
POST method; GET has limits on how much can fit because it goes into the
URL).

An example form with multiple variables would be:

<form action='php-page.php' method='post'>
<input type='text' name='userInput' maxlength='20' />
<input type='checkbox' name='checkboxArray[]' value='box1' />
<input type='checkbox' name='checkboxArray[]' value='box2' />
<input type='checkbox' name='checkboxArray[]' value='box3' />
<input type='hidden' name='globalVar' value='".$globalVariable."' />
<input type='submit' value='Click Here to Submit' />
</form>

The text type allows a user to put text into a text field; the checkbox
type allows multiple items to be selected and stored in the
checkBoxArray[] variable (notice that each checkbox element of the same
group must have the same name, and must end with [] square brackets);
the hidden type allows you to add unseen variables to a form so that
they don't interfere with the user, but they will be passed to the page
mentioned in action.

Hopefully this answers the questions you have raised in your posting.
However, there is much more to forms if you need more. Try www.w3c.org,
the home of WWW standards, for more form elements. www.htmlgoodies.com
is also very good. And, of course, the PHP manual.
--
Bob
London, UK
echo Mail fefsensmrrjyaheeoceoq\! | tr "jefroq\!" "@obe.uk"
Jul 17 '05 #2

P: n/a
"Titus Cheung" <ti***@ieee.org> wrote in message news:<WUbCb.671953$9l5.307287@pd7tw2no>...
Hello,

I want to write some code such that it'll update (ie insert, delete, etc)
some data to/from a mySQL database whenever a user hit an HTML form button
(ie Submit). Now what I find annoying is that this form can only, from my
understanding, execute a separate PHP file with the <form
action=removerow.php> code. Or can I call a function?

<snip> Also what I'm not sure if it is true or not. But it appears that whenever
PHP begins executing a different PHP file, all global variables that exist
from the previous PHP file are lost! Is this true? For example, all my
other variables information are lost when "removerow.php" executes.


This is correct. You can deal with this problem by using sessions.
Check out http://www.php.net/manual/en/ref.session.php

Tony Marston
http://www.tonymarston.net/
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.