Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple submits and capturing data

Status
Not open for further replies.

judi

Programmer
Jan 13, 2001
35
US
I have 2 questions:

1 - How do I display multiple submit buttons. The only way I've figured out to do it is with 3 different forms since the action of 2 of the buttons take you to other scripts, the third updates the current one. Is there a cleaner way to do it?

2 - How do I capture data that has been changed by the user if I want to stay in the current script? This is in ref to my update button and since PHP doesn't read what is on the client, how can I capture the data the user changed? Do I have to use Javascript or is there a way to fool PHP?

Thanks for your help.
 
No, use java. Its (in the long run) cleaner and far smarter for client side goodness.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
1. I assume you mean one HTML form with multiple buttons with different actions. What I do is name the submit buttons using the "name=" attribute of the name tag. The name of the clicked submit button will show up in the $_POST or $_GET superglobal array, so my script can know which button was clicked.

2. If you have a user that is presented with information he can optionally change, provide a copy of the original information separate from the version of the data the user might change. You can either place the original data in hidden form fields or use session variables. When the data is submitted to the script, the script can then compare the original data to the data that has passed through the user's hands.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
1 - Actually, I am now able to separate the different submits. I've used 3 input statements and I can check each one by the input name.

But now, I want to be able to go to another script. Since I can't do it on the form, how can I spawn another script in PHP?

2 - On this one, I want to be able to see the data that was inputed and manipulate it. One is a text input box and the other is in a select statement. I can't seem to be able to grab the values of either field.

Thanks for your help.
 
1. You don't necessarily have to spawn another script in PHP. In fact, if you're running PHP as a web-server module, I'm not sure how to do it.

If I have data that needs to be processed by multiple scripts, I take the processing guts of the script and move them to a separate file that all scripts can use via PHP's include() function.


2. How are you trying to reference the data? If you have a form named "foo" and you're trying to access a variable named "$foo" in your script, it may not work. And won't work with the default installation of newer versions of PHP. I strongly recommend referencing input values via PHP's superglobal arrays $_POST and $_GET (described here: There is a discussion of why you should use the superglobals here:
Want the best answers? Ask the best questions: TANSTAAFL!!
 
1: You can have as many submit buttons as you want in a form.
Code:
<input name=&quot;whatever&quot; type=&quot;submit&quot; value=&quot;Text on Button&quot;>
<input name=&quot;anotherone&quot; type=&quot;submit&quot; value=&quot;Text on Button #2&quot;>
However, this concerns more HTML than PHP.
Since you want to submit to different scripts you would also have to involve JavaScript to set the form's action attribute to the desired script.
Then the question arises: What about visitors who disable JavaScript?
If you set the form action attribute with JavaScript you can achieve what you want. Three forms seem cleaner, but there would also be JavaScript involved as not all three forms share the information entered.

2. You need not fool PHP. You should consider SESSION data. You can store the original data in the session variables and then compare the submitted data. Changes will then be evident by comparing the two arrays.

Just for clarity:
PHP runs on the server. The output is sent to the browser. In order to process the data from the browser a POST or GET requests is sent. Then PHP can start manipulating the submitted data. Sessions is a way to keep variables and values over successive accesses to PHP scripts.

Read about sessions:
 
Yea, I am storing data in session arrays. But for an input statement, I can't seem to be able to save the amount inputted til it's passed to the new PHP script. I wish I could do it from the current script. I'm having the same problem with a checkbox. I haven't used javascript yet. I'm sure I won't have trouble learning it, but I'm hesitant because a lot of people have it turned off.
 
It is completely correct that you cannot access information in PHP until it's submitted.

When a user points his browser at a PHP script, the web server runs the script and sends the data back to the browser. By the time the browser has rendered the page, the PHP script is no longer running.

Web apps are not continuous, like a console applicaiton. You have to think of a web application in terms of a set of discrete steps:[ol][li]user points browser at PHP URL, browser contacts web server, web server runs script[/li][li]script produces output and stops running[/li][li]web server streams output to browser, browser renders output[/li][li]user inputs values, user submits form[/li][li]Browser contacts web server and sends data[/li][li]web server runs script, passing data in from browser.[/li][li]script produces output and stops running[/li][li]loop to step 3[/li][/ol]

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Judi,

Again: You can't really call it the current script. When the page appears in the browser the PHP script has finished. You have to submit the page to make the session data aware of any changes and only the next PHP script can access and alter the session based data.

Client side scripting has no access to session data. Some of the set values in the form might have been set to the session data, the session manipulation however is only possible by PHP.

It's good to write code that works even if JavaScript is off. There are limits to what you do if you choose that route.
 
Thanks y'all for your help. While I've been a programmer since '86, this has been my first foray into web programming. I did 370 assembly language for a long time and then VB. So forgive me if I am a bit ignorant. LOL

Back to the multiple submit question. I know I can do an If statement for different buttons, but if I want each submit to go to another script, how do I do that? I tried putting a variable in the ACTION parameter of the Form tag, so it can have different values based on the submit I want, but it didnt work.

Any other suggestions?
 
It's not a matter of ignorance. I have no doubt you have the programming skills necessary. It's a matter of using the wrong metaphore.

In a VB GUI application, the GUI and the application code are all part of each other. The application code may wait for user input (and in fact spends something like 99.9999% percent of its time doing just that), but the application is currently running.

Web applications are more like the bad old days of punch cards. You run through the reader a set of cards which contain your program. Those cards may then record data on another set of cards. You then run another program from punch-cards, using the output data cards from the first program as the input data cards for your second program. Each step of the process is discrete. No program written by you is waiting for your input.

HTML is a output formatting specification, not a programming language, so the concept of an if-statement in HTML is meaningless.

Take the advice DRJ478 and I have both given you. Place two submit buttons on the form with different names. The name of the submit button clicked will be provided to PHP in the input stream. Have your PHP code perform one of multiple actions based on which submit-button name appears in the input stream.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
I understand that, but how do I send it to different scripts based on the button chosen? It just wants to go to the script named in the ACTION attribute of the form. I dont know how to get around it unless I use more then one form. Then it puts the buttons on different lines and it looks bad. Putting the ACTION into a variable didnt work.

dense judi
 
HTML doesn't support variables, only format strings. And a <form> tag can have only one &quot;action&quot; attribute, so it can send data to only one script.

There are ways to change the &quot;action&quot; attribute of a form, but only through JavaScript or VBScript, so that solution is outside the bailiwick of this forum.

Again, you don't need to submit to two different places -- you just need a script to be able to tell which button was clicked.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Ok, I do know how to tell which button is clicked. That's easy. But I do need two buttons to go to two different scripts. Do I have to use JavaScript for that?
 
No, you don't if you make the receiving script multi-funcional. I would look at the script you post to as the central receiving point that directs the request to the appropriate module for processing.
All you need is a structure like this - and you still can have your functions in disinct files:
Code:
<?php
# use module a when button A
if (isset($_POST['submitA']){
   include('./moduleA.inc.php');
}
# another button 'B'
if (isset($_POST['submitB']){
   include('./moduleB.inc.php');
}
....etc.

This makes JavaScript use unnecessary as the receiving script can decide by the button pressed what programming module to use. I have used tis central gateway approach in many applications. The include part makes the script only use the module needed and it only processes that module. It will ultimately be faster than having all functions in one file. Also, it seems easier to organize the modules in distinct files.
 
That worked, DR, but the scripts it goes to dispays another screen. When I use the include, it appends it to the bottom of the current screen rather then replacing the one already there.

This would be so much easier in VB. LOL

PITA judi
 
Let's say the functionality you need resides within scriptA.php, but the script has output functionality that you do not want to include.

Take the functionality from scriptA.php that you do need and move it to a file that can be included by both the script we're currently discussing and scriptA.php.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Judi,

The idea of the modular approach is to execute a module the same way you'd execute a function within a program. Modules can do all kinds of things, as you know.
Here are some examples:
- validation module: only included when form submitted. Checks out if all values are supplied in correct format. Continues on into main body if values are missing and generates main page with error message.

- processing modules: the different pages you are talking about. The only difference here is that the module ends with an exit statement to finish the branch.

You want to arrange the main script in a way that there is no output before the modules are accessed.
 
I guess I'm not getting the big picture. Here is what I have:
// Continue shopping was chosen
if (isset($_POST['continue'])) {
include('./displaycategories.php');
}
// Delete button was chosen
if (isset($_POST['deletechecked'])){
include('./ShoppingCart.php');
}

}
displaycategories is a whole new page I want to display.
It does this but it's appended to the end of the current page.

ShoppingCart has a self-submitting form. It's the current script I'm in. It calls itself. When I click this submit, it displays another version of the current page right below it. Then it goes to &quot;Page not Found&quot;

I can turn the delete into a function and then put an action on the form tag to go to my displacategories, but then, no matter which button I click, it goes to my display category routine instead of to the function.

If I write another script to do complete some actions, I don't know how to return to the orginal script without some action from the user.

Why am I not getting this? I feel so incredibly stupid. Sigh!




 
The additional HTML is not being appended to the end of the existing page you enter you data in. The script is running again.

It's just that the script is incorrectly running the code to produce the form, as well as the input processing code.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top