Multipe actions with one submit button in php html form

I have one php html form. Form is working fine but i have one issue on this line and flow of form
After entering value when i submit my form, my form open invoice.php in new tab which is absolutely fine. But the problem which I am facing is that form tab do not gave successful message by opening link. (In simple I am looking for to actions with my form with one submit button) First it should open invoice.php in new tab which its doing and 2nd is that form orignal tab should go to home page or successful after submitting data. here is my form code

<html>
<body>
<form method="POST" action="invoice.php"  target="_blank">
<label>Name</label><input type = "text" name = "name1" >
<label>Age</label><input type = "text" name = "age" >
<label>Gender</label><input type = "text" name = "gender" >
<label>Height</label><input type = "text" name = "height" >
<label>Education</label><input type = "text" name = "edu" >
<input type="submit" name="submit" id="submit" value="Submit"  class="btn btn-success submit_btn invoice-save-btm" >
</form></body> </html>~~~

Change the form so it submits to the same page. Be sure to add form processing at the top of the page before output to the browser (<html> etc) Be sure to put your processing code inside an IF condition checking for $_SERVER['REQUEST_METHOD'] === 'POST' and at least one more condition related to your form like !empty($_POST['name1']).

After processing, define your success message to a variable to be echoed on the page above the form… Be sure to wrap that message display section in a condition, like if not empty message, echo message.

Then at the end of your processing section add a header("refresh: ") with a 3 second delay and the url pointing to the home page.

If everything is done properly, the form will be processed, message created and shown to the user, then after 3 seconds they will go to the home page.

Note: I probably could have written the code faster than it took to write this out but I wanted to see how you do with these directions.

Respected sir thanks for your brief reply. But I think I failed to explain my question clearly. I want to open invoice.php and home page at the same time. (Home page can be open in same tab where as invoice.php open in new tab or new window of chrome. I done this with the help of java scrip as show below, but I want to do this without java script.


<label>Name</label><input type="text" name="name1">
<label>Age</label><input type="text" name="age">
<label>Gender</label><input type="text" name="gender">
<label>Height</label><input type="text" name="height">
<label>Education</label><input type="text" name="edu">
<input type="submit" name="submit" id="submit" value="Submit" class="btn btn-success submit_btn invoice-save-btm">

</form>

<script>
function redirectToHomePage() {

    window.location.href = '/';
    
    // Allow the form to continue submitting
    return true;
}
</script>```
1 Like

php does not have a method to open a new window, just direct user to a new page. If you really need to open a new window, use javascript at the end of your processing code.

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['name1'])) { 
	//processing code
	
	echo '<script type="text/javascript">  
		window.open("pathtohomepage", "_blank"); 
	</script>';
}
?>

BTW change your submit button name to something other than “submit”. Notice how I used ‘name1’ in the IF condition above. I would typically used a unique submit button name like “submit_form”, but using the name “submit” can be unreliable in MHO.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.