php multiple action on submit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

dkhanna01
Forum Newbie
Posts: 7
Joined: Tue Oct 13, 2009 4:05 pm

php multiple action on submit

Post by dkhanna01 »

I have a form which says:

<form enctype='multipart/form-data' method='post' action='process.php' target='_blank'>

I need to call process.php on submit or on clicking submit button. Now this process.php is opening in another page because of _blank which is what I wanted.

This is working absolutely fine, now i want this form to refresh / reload plus call process.php in new page, how can i make this work .

Thanks
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: php multiple action on submit

Post by Eric! »

Is this what you are saying: You want to hit submit, have a new window open display all your populated form data and run your php code? You could just redisplay your html form with the fields populated from your $_POST data and include the php code from process.php all the same page.

Are you trying to build a dynamic form? I usually use some in-line php in the form like this:

Code: Select all

<input type="text" name="login_name" value="<?php if(isset($login_name)) echo $login_name ?>"size="55" maxlength="20"></td></tr>
Then I put all that HTML into a seperate file and my PHP code will then control the form. For example form.php would check the query string via GET and if nothing is set, it displays the html via an include("form.html");

The form also sets the query string to tell the form.php what to do. For example

Code: Select all

<form name="feedback" action="form.php?process=submit" method="POST" >
This will then tell form.php that something new is happening.

The form.php file then handles these events using the switch/case

Code: Select all

<?php
  switch (@$_GET['process'])                                
  {
    case "submit":
    // put your code here to process the form
    // put another include "form.html" if you want to redisplay it
    break;
 
    case "whatever":       
    // you can make your form do other things too if you want.
    break;                                               
 
    default:                                             
        include("form.html"); //initial page display of form html
  }
?>
This then allows you to insert error statements into your your form.html which are good for popping up a warning next to a required field that was left blank. So in the case of "submit" say the login_name is blank, you then set an error like $message="ERROR: Your login name is blank." Then redisplay form.html. In form html you have a bit of in-line php code next to your fileds like this

Code: Select all

<?php
    if(isset($message))
        echo "<tr><td colspan='2'>$message </td></tr>";
?>
 
So that the message will be inserted into your form. You can also use highlighting or html codes to make the message stand out.....
dkhanna01
Forum Newbie
Posts: 7
Joined: Tue Oct 13, 2009 4:05 pm

Re: php multiple action on submit

Post by dkhanna01 »

Actually what I want is a very simple thing.

I have a form and when I hit submit I want to reload the same form plus I want to execute process.php in saparate window
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: php multiple action on submit

Post by Reviresco »

Add this to the body tag:

Code: Select all

<body onblur="document.forms[0].reset();">
(Assuming the form is the first form on the page -- adjust as necessary.)
dkhanna01
Forum Newbie
Posts: 7
Joined: Tue Oct 13, 2009 4:05 pm

Re: php multiple action on submit

Post by dkhanna01 »

Thanks it work in a way I wanted, but here is a small glitch. It is reseting the form but what I want is reload the form
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php multiple action on submit

Post by McInfo »

To reload the page when the form is submitted:

Code: Select all

<form action="process.php" target="_blank"
      onsubmit="window.location='<?php echo $_SERVER['SCRIPT_NAME']; ?>'">
 
    <input type="text" name="x" />
    <input type="text" name="y" />
    <input type="submit" />
</form>
To reset the form when the form is submitted:

Code: Select all

<script type="text/javascript">
function doSubmit (btnSubmit) {
    btnSubmit.wasClicked = true;
}
function doReset (btnSubmit) {
    if (btnSubmit.wasClicked) {
        btnSubmit.form.reset();
        btnSubmit.wasClicked = false;
    }
}
</script>
 
<form action="process.php" target="_blank">
    <input type="text" name="x" />
    <input type="text" name="y" />
    <input type="submit" onclick="doSubmit(this)" onblur="doReset(this)" />
</form>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:42 pm, edited 1 time in total.
dkhanna01
Forum Newbie
Posts: 7
Joined: Tue Oct 13, 2009 4:05 pm

Re: php multiple action on submit

Post by dkhanna01 »

I would like to reload and I cange my script to
<form enctype='multipart/form-data' method='post' action='process.php' target='_blank' onSubmit="window.location='<?php echo $_SERVER['SCRIPT_NAME']; ?>'">

But I think it is not reloading the page because I have a drop down window which gives me the name of all the branches, now in the form if I add any branch and submit the drop down list should give me this new branch got added. But the branch adding thing is happening in "process.php" script which I think is called after the page got reloaded so is there a way I can call process.php first and then reload the page.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php multiple action on submit

Post by McInfo »

Maybe something like this would work for you?

form.php

Code: Select all

<?php
$response = '';
include 'process.php';
?>
<p><?php echo $response; ?></p>
<form method="post" action="form.php">
    <input type="text" name="x" />
    <input type="submit" />
</form>
process.php

Code: Select all

<?php
// This file should not send any output to the browser
if (!empty($_POST)) {
    $response = 'Thank you for your submission.';
}
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:43 pm, edited 1 time in total.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: php multiple action on submit

Post by Eric! »

Which is pretty much what I was suggesting, just without passing conditions to the php code.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php multiple action on submit

Post by McInfo »

Eric! wrote:Which is pretty much what I was suggesting, just without passing conditions to the php code.
Sorry, Eric!. I was answering "in the moment" and forgot that you had made a similar suggestion.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:43 pm, edited 1 time in total.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: php multiple action on submit

Post by Eric! »

Nothing to appologize for! My comment wasn't really directed at you, McInfo...I was just pointing it out to dkhanna01 in case he wants to compare them. Your example is simplier and probably easier to follow.
dkhanna01
Forum Newbie
Posts: 7
Joined: Tue Oct 13, 2009 4:05 pm

Re: php multiple action on submit

Post by dkhanna01 »

hmmm .. including process.php is opening php page on the same page where form is and not executing the process.php in saperate window
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: php multiple action on submit

Post by Eric! »

Maybe you need to explain what you want again. I asked this earlier, is this the idea:
Eric! wrote:Is this what you are saying: You want to hit submit, have a new window open display all your populated form data and run your php code? You could just redisplay your html form with the fields populated from your $_POST data and include the php code from process.php all the same page.
dkhanna01
Forum Newbie
Posts: 7
Joined: Tue Oct 13, 2009 4:05 pm

Re: php multiple action on submit

Post by dkhanna01 »

What I want is.. when I hit submit 1.Execute process.php in NEW window. and 2. I should be able to reload the same form (but not in new window .. same window)

So process.php in new window and also reload a form in same window but I need to execute process.php firstand then reload form in order of execution.

Thanks
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: php multiple action on submit

Post by Reviresco »

What do you mean by "reload the form"?

The javascript location.reload() method will reload the entire page.

If you want to just clear the form, use reset().

If you want the user input to remain in the form, you don't need to do anything -- it will just remain there when you open the new window.
Post Reply