I want to link 2 php files together by clicking a button - php

I have 2 php files and I want to link the two files by clicking a button .. so if I click a button in the first php file it should transfer my to the second php file ..
1st php file and here I want to click s & P Database button :
<html>
<body>
<h1> My System </h1>
<form action="">
<input type="button" value="S & P Database ">
<input type="button" value="Generate Report ">
</form>
</body>
</html>
2nd php file that I should have it when the button is clicked :
<html>
<body>
<form action="">
<h4><b>Please choose one of the following options below : </b> </h4>
<input type="radio" name="option" value="search" /> Search<br/>
<input type="radio" name="option" value="open database" /> Open Database<br/>
<input type="radio" name="option" value="administrative page "/> Administrative Page <br/>
</form>
// This button doesn't appear in the web page =( .. dunno why
<form action="">
<input type="button" value="Choose ">
</form>
</body>
</html>

If I understand correctly, you want to redirect the user to the second page when the button on the first page is clicked.
To simply redirect the user, you can use the button's onclick:
<input type="button" value="S & P Database" onclick="window.location.href='page2.php'" />

<input type="button" onClick="window.location='/2nd.php'" value="S & P Database ">​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
This will redirect you to /2nd.php

You need to add link to php nahdler in action attribute
so it will be <form action="http://www.website.com/1.php">
Input type should be "image" or "submit", button will not submit your form.
You can also submit form by JS, but i will not describe how to do that here.
Use right document structure and doctype: http://www.rantiev.com/doctypes/
Check your code with validator: http://validator.w3.org/#validate_by_input
When some elements dissapear it can be that HTML have errors.

There are several ways to do this:
2 forms submitting to different locations
<html>
<body>
<h1> My System </h1>
<form action="page2.php" method="get">
<input type="button" name="pressed" value="S & P Database ">
</form>
<form action="page3.php" method="get">
<input type="button" name="pressed" value="Generate Report ">
</form>
</body>
</html>
2 links to different locations
<html>
<body>
<h1> My System </h1>
S & P Database
Generate Report
</body>
</html>
1 form with name and values of buttons. But your script (page2.php) will need to check the value of $_GET['pressed'] too
<html>
<body>
<h1> My System </h1>
<form action="page2.php" method="get">
<input type="button" name="pressed" value="S & P Database ">
<input type="button" name="pressed" value="Generate Report ">
</form>
</body>
</html>
BUT dont forget to urldecode($_GET['pressed'])) as the values will be encoded.

simple Put
method="post" or method="Get"
and in action type the name of 2nd file which in your case will be
action="2ndfile.php(what ever its name is)"
also in cahnage your button to submit
keep in mind that this action script is for your 1st file.

Related

Issues calling php scripts in HTML

I am running into a strange error on a website using multiple PHP scripts. For some reason, every submit button only calls the first PHP script defined rather than the one chosen. I know all of these scripts work and this issue started only recently. Here is the code in question:
<!DOCTYPE html>
<html>
<head>
<title>Embed PHP in a .html File</title>
</head>
<body>
<h2>POV</h2>
<form action="index.php">
<button type="gohome">Return to main form</button>
</form>
<h3>WIP Final results:</h3>
<br>
<?php
include("showdatabasecontents.php");
?>
<form method="post" action="clearFinal.php">
<input type="submit" name="clearFinal" value="Clear Responses">
<form method="post" action="resetFinal.php">
<input type="submit" name="resetFinal" value="Reset ID Count">
<h3>Students names</h3>
<?php
include("showdatabasecontent2.php");
?>
<h3>Add a Student</h3>
<form method="post" action="addstudent.php">
Student Name : <input type="text" name="studentname"><br><br>
<input type="submit" name="addstudent" value="Submit">
</form>
<h3>Delete a student</h3>
<form method="post" action="connect.php">
ID : <input type="text" name="id"><br><br>
<input type="submit" name="removeStudent" value="Submit">
<br>
</form>
</body>
</html>
I have tried changing some of the names for the buttons to make sure there was not a conflict but that did not make any different. Any info on this issue will help, thanks!
try to add an id to each form. and change
<input type="submit" name="" value="">
to
<button type="button" onClick="test()">Submit</button>
example
<h3>Delete a student</h3>
<form id="deleteStudent" method="post" action="connect.php">
ID : <input type="text" name="id"><br><br>
<button type="button" onClick="test()">Submit</button>
<br>
</form>
last add javascript
<script>
function tes(){
document.getElementById('deleteStudent').submit()
}
</script>
You cannot have nested forms. Please close your forms properly and retry
Found the issue, at the advice of Quentin, I used https://validator.w3.org/. I came to the conclusion based on the information I drew that I was missing a closing tag for two form elements. I would mark this as solved but stackoverflow won't let me for two days :(

Change web page contents before it gets displayed to the user using php

buttons.php
<!DOCTYPE html>
<head>
<title>
Button Page
</title>
</head>
<body>
<button name="btn1">Button 1</button>
<button name="btn2">Button 2</button>
<button name="btn2">Button 3</button>
</body>
</html>
and the result.php
<!DOCTYPE html>
<head>
<title>
Result Page
</title>
</head>
<body>
<p class="parag1">This value is for Button1</p>
<p class="parag2">This value is for Button1</p>
<p class="parag3">This value is for Button1</p>
</body>
</html>
what I want to happen is when the user clicks a button, the paragraphs in the result.php will change to values assigned for that button before it gets displayed to the user. Thanks!
First off I would recommend looking into PHP POST and GET methods, they will give you a more complete picture of how form data can be passed from one page to another.
Regarding your question all you would need to do here is wrap each button in a form like so:
<form action="results.php" method="POST">
<button name="btn1">Button 2</button>
</form>
<form action="results.php"method="POST">
<button name="btn2">Button 2</button>
</form>
<form action="results.php" method="POST">
<button name="btn3">Button 3</button>
</form>
I would recommend using a hidden value that is readonly and changing your button to a submit, this will allow you to hide the values you would like to pass across and stop unwanted editing of the values by the users.
e.g.
<form action="results.php" method="POST">
<input readonly type="hidden" name="buttonValueOne" value="Button 1"/>
<input type="submit" name="btn1">Button 1/>
</form>
<form action="results.php"method="POST">
<input readonly type="hidden" name="buttonValueTwo" value="Button 2"/>
<input type="submit" name="btn2">Button 2/>
</form>
<form action="results.php" method="POST">
<input readonly type="hidden" name="buttonValueThree" value="Button 3"/>
<input type="submit" name="btn3">Button 3/>
</form>
Then on the results page, you would use if statements to check which one was passed over:
if (isset($_POST["buttonValueOne"]) { ?>
<p class="parag1"><?php echo $_POST['buttonValueOne']; ?></p>
<?php
}
?>

PHP - Can't listen for submit button press

I am trying to build a simple HTML form that is spread over two pages.
First page asks for user's first and last name. Second page asks for his country & whether he would like to receive additional info.
The first page has a single button named "Next" for going to the second page. The second page has two buttons named "Next" to go the next page (non-existent at present) and "Back" to go back to the first page.
On the first page when I click the "Next" button, nothing happens. How can I go to the next page on clicking it ?
Here is my code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
if(!isset($_POST['next1']))
Display1();
else if(isset($_POST['next1']))
Display2();
else if(isset($_POST['back1']))
Display1();
?>
<?php
function Display1()
{
?>
<form id="form1" action="Multi.php" method="post">
<label>Enter First Name: </label>
<input type="text" name="text1" />
<br />
<br />
<label>Enter Last Name: </label>
<input type="text" name="text2" />
<br />
<br />
<input type="button" name="next1" value="Next" />
<?php
}
function Display2()
{
?>
<form id="form2" action="Multi.php" method="post">
Select Country
<select name="country[]" multiple="multiple">
<option value="India">India</option>
<option value="USA">USA</option>
<option value="China">China</option>
</select>
<br />
<br />
Do you want to receive latest info ?
<input type="checkbox" name="checkbox1[]" />Yes
<br />
<br />
<input type="button" name="next2" value="Next" />
<input type="button" name="back1" value="Back" />
<?php
}
?>
</body>
</html>
The input type needs to be "submit"
<input type="submit" name="next1" value="Next" />
So for all the inputs that has "button" you need to change to "submit"
And as an addition to Abhik's answer.
<input type="button"> is only a clickable input. You (mostly) use it when you want to call a JS script. So if for some obscure reason you want to use this instead of the more simple solution provided by Abhik go ahead and add a onclick event handler.
<input type="button" onclick="document.getElementById("form1").submit();">
You could also use a <button> instead, but make sure you always specify the type. The advantage is that they offer a higher degree of customization. Basically you can put HTML inside a <button> tag.
So try also:
<button type="submit" value="Next">Next</button>
The other types of buttons are (source: w3schools.com):
button: The button is a clickable button
submit: The button is a submit button (submits form-data)
reset: The button is a reset button (resets the form-data to its initial values)

My Form Gets submitted on click of any button out of 2

no there is no relation between show all and display function.
even i commented the code inside ShowAll function still form gets submitted.
Is there anything related to POST event getting fired when I click on the buttons ?
I also did 1 change today, as I removed the 2nd button code outside the Form Tag Body
OLD code
<form name="form2" method="post" action= "submitlogin.php">
<td class="label"><br><input type="submit" name="submit" value="Submit-" size="25" onclick="display()"></td>
<button id='button' onclick="ShowAll()" >Click </button>
</form>
New Change
<form name="form2" method="post" action= "submitlogin.php">
<td class="label"><br><input type="submit" name="submit" value="Submit-" size="25" onclick="display()"></td>
</form>
<button id='button' onclick="ShowAll()" >Click </button>
// Now ShowAll stopped working once its outside the Form Tag Body

Performs form action even out of the form

Hi having a problem with this code even if i clicked on cancel it will still proceed to use the action of my form even though the CANCEL button is not part of the form, Would appreciate any help.
Here is the code.
<form method="post" action="input_enroll.php" >
<div id="overlay1">
<div>
<h1> Enter Educational Level Entry</h1>
<input type="text" name="level" >
<input type="submit" value="Proceed To Enroll" '>
</form>
<input type="submit" value="Cancel" onclick='overlay()'>
</div>
</div>
EDIT: Any suggestions what would be a better idea? I'm thinking putting the cancel outside the div.
You are having form started, then divs started, then form closed..start form after divs..
as your markup is not correct, browsers will change it as thier parser suggest,
In chrome </form> tag is postponed after </div>s..
<div id="overlay1">
<div>
<form method="post" action="input_enroll.php" >
<h1> Enter Educational Level Entry</h1>
<input type="text" name="level" />
<input type="submit" value="Proceed To Enroll" />
</form>
<input type="submit" value="Cancel" onclick='overlay()' />
</div>
</div>

Categories