Issues calling php scripts in HTML - php

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 :(

Related

Append input from search box into button link

I'll make this short and quick. :)
I'm trying to implement a button on my website, which redirects me to a URL I specify + what the user is looking for.
I have this little piece of code here:
<html>
<div class="search">
<form role="search" method="get" id="search">
<div>
<input type="text" value="" name="tag_search" id="tag_search" />
<input type="button" value="Cerca" onclick="window.location.href='https://mywebsite.com/'" />
</div>
</form>
</div>
</html>
that is almost working.
The only thing is that if the User inputs "Hello" in the Search Box, it will always search for the following URL once you press the "Submit" button: https://mywebsite.com/
How can I append what the User has written into the Search Box, so that the Button will redirect me to: https://mywebsite.com/Hello ?
Thank you all!
Add the value of the input to the link
<html>
<div class="search">
<form role="search" method="get" id="search">
<div>
<input type="text" value="" name="tag_search" id="tag_search" />
<input type="button" value="Cerca"
onclick="window.location.href='https://mywebsite.com/' +
document.getElementById('tag_search').value" />
</div>
</form>
</div>
</html>
Add the following Javascript to help
<script>
function goToSearch() {
window.location.href= 'https://mywebsite.com/' + encodeURI(document.getElementById("tag_search").value)
}
</script>
Then your HTML should look like this
<html>
<div class="search">
<form role="search" method="get" id="search">
<div>
<input type="text" value="" name="tag_search" id="tag_search" />
<input type="button" value="Cerca" onclick="goToSearch()" />
</div>
</form>
</div>
</html>

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
}
?>

Input in HTML not connecting to PHP post method

I have an HTML input and button:
<form action="validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<form>
<label></label>
<input id="input" name="InputText" type="text"/>
</form>
</div>
<!-- THE BUTTON ITSELF -->
<input type="button" id="button" name="myButton"><b>Search Archive</b>
</form>
in my validate.php file I have this switch statement:
<?php
switch ($_POST["InputText"])
{
case "someval":
http_header("someaddress.com");
die();
break;
}
?>
the problem is that when I click the button it doesn't do anything. I did this with JS and it worked but it should be noted that I'm really new to web development so if anyone can explain to me what I did wrong and specifically why that would be great. Thanks!
You have a form inside of a form, that won't work. Also, you need to include an <input type="submit" value="submit" /> before you close your form. This is what submits the information from the form to your action="file.php".
A form would typically look like this:
file.html
<form action="validate.php" method="POST">
<input type="text" name="username" placeholder="Enter your username" />
<input type="submit" name="submit" value="Submit" />
</form>
Then you'd do something like this:
validate.php
<?php
echo "Your username is" . $_POST['username'];
The $_POST['username'] is the data gathered from the name="username" input from the HTML. If you write die($_POST); you'll get all the data that is sent through the form.
When you are using type='button' you have to perform the submit by yourself.
So, you can do that using javascript or change to type='submit'.
Example:
<input type="button" id="button" name="myButton"><b>Search Archive</b>
To
<input type="submit" id="button" name="myButton" value="Search Archive" />
you can try this
<form action="/validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<label></label>
<input id="input" name="InputText" type="text"/>
</div>
<!-- THE BUTTON ITSELF -->
<button type="submit" id="button" name="myButton">Search Archive</button>
</form>
in the div id ="code" you used form tag that's why its not work...delete it will work and button type must be submit

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>

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

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.

Categories