I have installed XAMPP to simulate a php server environment on my pc. Unfortunately my html form will do nothing when I hit submit. Here is html:
<html>
<form>
<form action="http://localhost/New/nn.php" method="post">
Why don't they play poker in the jungle?<br>
<input type="radio" name="jungle" value="treefrog"> Too many tree
frogs.<br>
<input type="radio" name="jungle" value="cheetah"> Too many cheetahs.
<br>
<input type="radio" name="jungle" value="river"> Too many rivers.<br>
<br>
<input type="submit" name="submit" value="Submit"><br>
</form>
</html>
Here is php:
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['jungle'])) {
if ($_POST['jungle']=="cheetah") {echo "You got the right
answer";}
if($_POST['jungle']=="treefrog"){echo "You selected treefrog but
answer is cheetah\n";}
if($_POST['jungle']=="river"){echo "You selected river but
answer is cheetah\n";}
}
else { echo "You did not choose an answer for jungle.\n"; }
}
else { echo "Please submit the form."; }
?>
In the code provided, you do have two form opening tags:
<form>
<form action="http://localhost/New/nn.php" method="post">
That might be a source of your problem.
On chome for mac (latest version).
Right click on your page, inspect
Go to the Network tab and reload
Click on your file name (for me test.html) and go to the preview tab. See how you have both tags
Now back on the Elements tab, see how you have only the first one, hence sending you back to the same page (that is the default action)
Related
I'm trying to use a tag inside of a function. In this case I want to click the button "find" and display the message "You selected the FIND option!" but it doesn't works. Can someone give me a hint or help me? I'm new using PHP. MORE DESCRIPTION: The website shows me the submit buttons "fruit" and "vegetable". When I click the fruit button then the website displays a table with products and one button called "find". If the user clicks the "find" button then the website must display the message "You selected the FIND option"(this message is just for testing). The problem here is when I click the "find" button, the website doesn't display the message.
<form action="Supermarket.php" method="post">
Please choose which one are you looking for:
<input type="submit" name='fruit' value="fruit">
<input type="submit" name='vegetable' value="vegetable">
</form>
<?php
if(isset($_POST['fruit'])){
fruitMenu();
}
if(isset($_POST['vegetable'])){
vegetableMenu();
}
function fruitMenu(){
echo "--------Welcome to fruit section--------";
$fruits = array(array('Name'=>"Apple",'ID'=>1234),
array('Name'=>"Banana",'ID'=>5678),
array('Name'=>"Watermelon",'ID'=>91011),
array('Name'=>"Orange",'ID'=>1213),
array('Name'=>"Mandarina",'ID'=>1415),
array('Name'=>"Pera",'ID'=>1617));
displayTable($fruits);
echo "<h2>WHAT DO YOU WANT TO DO?</h2>";
displayOptions();
}
?>
<?php
function displayOptions(){
echo '<form action="Supermarket.php" method="post">
<input type="submit" name="find" value="find">
</form>';
if(isset($_POST['find'])){
echo "You selected the FIND option!";
}
}
?>
I am very new to wordpress. I am trying to do the following:
a. I have a very simple html and php based website. I have a signup page which accepts user id and password and then on click of submit button it calls a php file which verifies the userid/password and saves it in the database. THIS WORKS PERFECTLY FINE
b. Now, I trying to move those two pages to wordpress platform. I have installed a php plugin also. Now the problem in wordpress platform is whenever I hit the submit button, in the database 3 duplicate records are getting appended. I have been trying a lot but could not fix this
c. I tried different php plugins but does not work
d. After reading few help documents on the web, I have tried creating my own template page, even that does not work
Any help in this regard will be much appreciated.
EDIT
Form:
<form action="http://xxxxxx.xxx/xxxxxx" method="post">
Enter User ID:
<input name="userid" type="text" value="" />Password:
<input name="password" type="text" value="" />
Confirm Password:
<input name="confrimpassword" type="text" value="" />
<input type="submit" value="SIGN UP" />
</form>
Below is the php file:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
//echo "Favorite color is ".$_SESSION["favcolor"].".<br>";
//echo "Favorite animal is ".$_SESSION["favanimal"].".";
?>
<?php
mysql_connect("localhost","***********","**************");
mysql_select_db("******");
$sql1=mysql_query("select * from login_details where customer_id='$_POST[userid]'");
$row=mysql_fetch_assoc($sql1);
if (!$sql1) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
//exit;
}
else {
echo " inside last else";
$sql2=mysql_query("INSERT INTO login_details VALUES ('$_POST[userid]','$_POST[password]')");
echo "Account successfully Created...";
}
mysql_close();
?>
</body>
</HTML>
I am new to both HTML and PHP and I encountered a problem when working on some simple projects. Lets say I have a text bar on my webpage and I want to display the text written in text bar on webpage after the user enters some text and presses the submit button. My problem is that the webpage shows the output when the webpage first loads. Is there a way to prevent the php code from executing untill the submit is pressed ?
Here is a sample code code that indicates the problem I am referring to.
<html>
<body>
<form action="./index.php" method="GET">
First Name: <input type="text" name="first" maxlength="50"><br/>
<input type="submit" value="GO"/>
</form>
</body>
</html>
<?php
$text_var = $_GET[first];
echo "This was typed into text bar" . $text_var;
?>
So "This was typed into text bar" is outputted right away when website loads.
I want it to be outputted only after submit button is pressed.
Thanks.
you need to split it up the form should be showed if nothing is submited so either check the value or the submit button
make sure you keep the html format. look at label tags to describe form inputs
<html>
<body>
<form action="./index.php" method="GET">
<label for="first">First:</label>
<input id="first" type="text" name="first" maxlength="50"><br/>
<input type="submit" value="GO"/>
</form>
<?php
if (!empty($_GET['first'])) {
//take care you escape things never output user input (XSS)
$op = htmlspecialchars($_GET['first']);
echo "This was typed into text bar" . $op;
}
?>
</body>
</html>
Check if $_GET['first'] exists. This is is usually done as the following:
View
<form action="index.php" method="post">
<!-- input fields here -->
<input type="submit" name="submit" value="GO"/>
</form>
Controller
<?php
if (isset($_POST['submit'])) {
// process post
} else {
// display the form
}
<?php
if(isset($_GET['submit'])){
$text_var = $_GET[first];
echo "This was typed into text bar" . $text_var;
}
?>
I have a question related to php. If anyone have an idea,please share with me. (I am a beginner in php).
I received a button value from an HTML page and displayed corresponding figure in the second page using code "param1.php". In the same program itself ( ie, "param1.php"), there is a set of radio box and i need to receive the radio value using another php program. But here i have confusion how to receive the radio value using another php program say "param2.php". also how the page redirect (to the php program where i receivvee the radio value) on selecting a radio button.
Thanks
I attach the "param1.php" below,
<!DOCTYPE html>
<html>
<body>
<br>
<?php
$data = $_POST['btn'];
$data2=$data.".png";
?>
<img src="<?php echo $data2;?>">
<h3>SCALE</h3>
<input type="radio" name="group1" value="5,10,15" checked> 5,10,15<br>
<input type="radio" name="group1" value="5,9,13"> 5,9,13<br>
</body>
</html>
No form tags
There are no form tags, so your radio tags won't get POST'd to param2.php
<form action="param2.php" method="POST">
<input type="radio" name="group1" value="5,10,15" checked> 5,10,15<br>
<input type="radio" name="group1" value="5,9,13"> 5,9,13<br>
<input type="submit" value="Process" />
</form>
Incorrect $_POST key
Now, in your param2.php file, you will be able to get the value of POST['group1']. In your code you're using the wrong key. btn doesn't exist within $_POST (in the code you've given anyway). The radio button name is group1, so access it as such;
$data = $_POST['group1'];
The value of $data will either be 4,10,15 or 5,9,13. Ensure you've got an image named 4,9,13.png and 4,10,15.png else your image won't show. Because they're comma separated, I'm going to assume these are unique file names. So;
foreach( explode(",", $_POST['group1']) as $file) {
echo "<img src='". $file .".png' />";
}
Also, ensure you do some checks on the posted data to validate the user input
I am trying to create a registration website where the users chooses amongst three options on the first page and after selecting, can move onto the second page where it is displays different information depending on the option selected previously.
On Registration_1.php this is the code:
<?php
$clicked = $_POST["Next"];
if(isset($_POST['Reg_type']))
{
header('Location:Registration_2.php');
}
elseif(isset($_POST['Next']))
{
header('Location:Registration_1.php');
echo "Error! You must select an option!";
// display form again here
}
?>
<form name="frmtype" action="Registration_2.php" method="post" >
<input type="radio" name="Reg_type" value="1"/> Registering myself with credit card or bank account <br/>
<input type="radio" name="Reg_type" value="2"/> Registering multiple people using credit card or bank account <br/>
<input type="radio" name="Reg_type" value="3"/> Registering multiple people using a purchase order <br/>
<input type="submit" name="Next" value="Submit"/>
</form>
How can I redirect the user back to this page if they simply click submit without choosing an option and possible display a error message, and send them to page 2 if they choose an option? Thank you
Edit: Added if block, but still moved to next page regardless of selecting an option or not. Is the header() not the correct method to move to another page?
if(isset($_POST['Reg_type']))
{
// execute some code
// go to page 2
}
else
{
echo "Error! You must select an option!";
// display form again here
}
Documentation: $_POST variable