Cannot echo variables after form submittiom - php

So I am submitting a form to the same page. I have used the following code to check whether the form was submitted, and to avoid 'undefined variables' errors during the first page reload.
if (isset($_POST['submit'])){
// Get search variables
$pName = $_POST['pname'];
$pLocation = $_POST['plocation'];
$pPrice = $_POST['pprice'];
if (isset($_POST['ptype'])){
$pType = $_POST['ptype'];
}
echo "pType";
}
However, I cannot echo the php variables onto the page. I am guessing this is because the form is refreshed when it is sent by post, so the variables are lost.
How can I fix this problem?
This is the html form
<form method="post" action="../html/searchpage.php">
<div id="searchborder">
<input type="text" id="pname" name="pname" placeholder=" Property name">
<input type="text" id="plocation" name="plocation" placeholder=" Property location">
<input type="number" id="pprice" name="pprice" placeholder=" Property price">
<div id="ptypeholder">
<div id="ptypebox">
<select name="ptype">
<option value="" disabled selected>Post type</option>
<option value="buy">Buy</option>
<option value="rent">Rent</option>
</select>
</div>
</div>
<button type="submit"><img src="../images/search.png"></button>
</div>
</form>
I am trying to display 'property posts' into html cards from a database using php in this page. The form is the search bar for the property posts.
Thanks!

I don't think $_POST['submit'] is being set because the button needs a name ie
<input type="submit" name="submit" value="submit">
also you have a few other syntax errors, structure, may be this will help :-
<form method="post" action="searchpage.php">
<input type="text" id="pname" name="pname" placeholder="Property name">
<input type="text" id="plocation" name="plocation" placeholder="Property location">
<input type="number" id="pprice" name="pprice" placeholder="Property price">
<select name="ptype">
<option value="" disabled selected>Post type</option>
<option value="buy">Buy</option>
<option value="rent">Rent</option>
</select>
<input type="submit" name="submit" value="submit">
and you don't HAVE to assign the post values to $vars you can use them directly -
<?php
if (isset($_POST['submit'])){
// Get variables
//$pName = $_POST['pname'];
//$pLocation = $_POST['plocation'];
//$pPrice = $_POST['pprice'];
echo $_POST['ptype'].'<br>';
echo $_POST['pname'].'<br>';
echo $_POST['plocation'].'<br>';
echo $_POST['pprice'].'<br>';
}
?>
Needs more work to make it check all posts are set or validation but works as a basic starting point
Also in original you posted echo "pType"; will only echo the text pType not the value

Related

Reading Data from two PHP pages into the third Page

I'm not able to combine the data from two pages. I'm only able to show data from one page. I tried searching for an explanation of my problem on google but I could not find it.
I get an "Undefined index" error as you can see [![in this screenshot][1]][1].
Could not put just code over here because it keeps telling me that I need to use spacing with ctrl + K and im newbie in all this things so please forgive me..
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
</head>
<body>
<form action = "page2.php" method = "POST">
<select list="Country" placeholder="Country" name="country" required class="form-control" style="max-width:250px; margin-top:50px;" id="Country">
<option >Bosnia & Herzegovina</option>
<option >Croatia</option>
<option >Serbia</option>
<option >England</option>
<option >Germany</option>
<option >Austria</option>
<option >Belgium</option>
<option >Switzerland</option>
<option >Italy</option>
<option >Romania</option>
<option >France</option>
<option >Montenegro</option>
<option >Slovenia</option>
</select>
<input type="email" name="email" id="email" placeholder="Your email address" required class="form-control" style="max-width:250px;" />
<input type="password" name="password" id="creapass" placeholder="Create your password" required class="form-control" style="max-width:250px;" />
<input type="password" name="password2" id="password" placeholder="Confirm your password" required class="form-control" style="max-width:250px;" />
<input type="button" value="Next" class="btn btn-primary" style="width:150px;">
</form>
</body>
</html>
Second Page
<!DOCTYPE html>
<html>
<head>
<title>test2</title>
</head>
<body>
<form action = "finalpage.php" method = "POST">
<!-- Values from First Step -->
<input type="hidden" name="country" value="<?php $_POST['country'] ?>">
<input type="hidden" name="email" value="<?php $_POST['email'] ?>">
<input type="hidden" name="password" value="<?php $_POST['password'] ?>">
<input type="hidden" name="password" value="<?php $_POST['password2'] ?>">
<!-- End of Values from First Step -->
<?php
echo "Country:" .$_POST["country"]."</br>";
echo "Email:".$_POST["email"]."</br>";
echo "Password:".$_POST["password"]."</br>";
echo "Password2:".$_POST["password2"]."</br>";
?>
<input type="text" name="first" class="form-control" placeholder="First name" required style="max-width:250px;" >
<input type="text" name="last" class="form-control" placeholder="Last name" required style="max-width:250px;" />
<input type="date" name="date" class="dateb" id="dateOfBirth" required />
<select list="Country" placeholder="Country" name="country2" required class="form-control" style="max-width:250px; " id="Country">
<option >Bosnian</option>
<option >Croat</option>
<option >Serb</option>
<option >English</option>
<option >German</option>
<option >Austrian</option>
<option >Belgian</option>
<option >Swiss</option>
<option >Italian</option>
<option >Romanian</option>
<option >French</option>
<option >Montenegrin</option>
<option >Slovenian</option>
</select>
<input type="text" class="form-control" name="street1" placeholder="Street address 1" required style="max-width:250px;" />
<input type="text" class="form-control" name="street2" placeholder="Street address 2 (Optional)" style="max-width:250px;" />
<input type="text" class="form-control" name="city" placeholder="City" required style="max-width:250px;" />
<input type="text" class="form-control" name="region" placeholder="Province/Region" required style="max-width:250px;" />
<input type="text" class="form-control" name="postal" placeholder="Postal code" required style="max-width:250px;" />
<input type="tel" class="form-control" name="phone" placeholder="Phone number" required style="max-width:250px;" />
<input type="checkbox" name="terms" > I have read and agree to the Terms and WBC's User Agreement.
<input type="Submit" name="insert" value="Continue" class="btn btn-primary btn-lg">
</form>
</body>
</html>
Final Step.
<html>
<head>
<title>get data from another page</title>
</head>
<body>
<?php
echo "Country:" .$_POST["country"]."</br>";
echo "Email:".$_POST["email"]."</br>";
echo "Password:".$_POST["password"]."</br>";
echo "Password2:".$_POST["password2"]."</br>";
echo "First name:" .$_POST["first"]."</br>";
echo "Last name:".$_POST["last"]."</br>";
echo "Date:".$_POST["date"]."</br>";
echo "Country2:".$_POST["country2"]."</br>";
echo "Street1:" .$_POST["street1"]."</br>";
echo "Street2:".$_POST["street2"]."</br>";
echo "City:".$_POST["city"]."</br>";
echo "Region:".$_POST["region"]."</br>";
echo "Postal code:" .$_POST["postal"]."</br>";
echo "Phone number:".$_POST["phone"]."</br>";
?>
</body>
</html>
I am not copying the entire code of yours as it is not required. Below is the logic you have to follow.
In the first step, i.e. your first form (I prefer using PHP format as it allows you to use PHP functions, if required), put your second PHP in the form's action.
<form action = "step_2.php" method = "POST">
Now in your step_2.php, you have to add the below in your form.
<!-- Values from First Step -->
<input type="country" name="country" value="<?php echo $_POST['country'] ?>">
<input type="hidden" name="email" value="<?php echo $_POST['email'] ?>">
<input type="hidden" name="password" value="<?php echo $_POST['password'] ?>">
<!-- End of Values from First Step -->
Above code will add the values posted from the first step into your second step form. Now you can simply fetch these values in the final step. To do this, add your final step PHP in your Action of this form.
<form action = "final_step.php" method = "POST">
In your final_step.php, you can normally get the values by $_POST.
You've got a number of problems. First, your option fields don't have values.
What you have: <option>Bosnia</option>
What you need: <option value="bosnia" >Bosnia</option>
You should also have a default as your first option, otherwise your program is going to assume that everyone who doesn't pick something has the first option, and you are going to get a million submissions that are not accurate. Eg:
<option value="false" selected="selected">Please select an option from below</option>
Then your backend should be checking like this:
<?php
if (!isset($_POST['country'] || $_POST['country'] === 'false')
{
//They did not pick a country, and you need to bounce them back to the form with an error message.
header('Location: /path/to/your/form');
exit;
}
//country is defined, you can continue,
//but you should pre-validate the other required field here also.
//do not assume things are right, assumptions = bugs.
...
The anchor tag you have surrounding your submit button may be redirecting instead of submitting the form, in which case your $_POST will be completely empty. The below tag you have:
<input type="button" value="Next" class="btn btn-primary" style="width:150px;">
Needs to look more like this:
<input type="submit" value="Next" class="btn btn-primary" style="width:150px;">
Or alternately:
<button type="submit" class="btn btn-primary" style="width:150px;">Next</button>
If you are stumped, put this at the beginning of what you have on your processing code:
echo '<pre>' . print_r($_POST, 1); exit;
This will show you what was submitted, and let you work through from there as needed.
In general, your conditionals should check if a key exists before validating it if there is any ambiguity as to whether or not it is set:
//BAD
if ($_POST['country'] == 'someval') { ... }
//GOOD
if (array_key_exists('country', $_POST) && $_POST['country'] == 'someval') { ... }
You can also check with isset($_POST['country']); HOWEVER, if null values are valid for whatever you are checking, then this will return false, even if the key is there. So in any case where null is valid, you need to use array_key_exists() instead.
Instead of passing variables directly from one page to another, it's a lot easier if you use $_SESSION, so if they derp up and hit back or walk away for a while, the values are not entirely lost. Users hate having to redundantly redo things, and will probably entirely lose interest in your site if they have to do that. Instead, you can set the values if they exist into session memory like this:
<?php
session_start(); //you have to do this before printing ANY other content.
$_SESSION['country'] = (isset($_POST['country']) ? $_POST['country'] : null);
And then you can retrieve this at any time from any page by doing:
<?php
session_start();
echo $_SESSION['country'];
and if you want to clear them, you just do:
<?php
session_start(); //can't destroy a non-existent session, you have to do this first
session_destroy(); //clears the session
session_start(); //starts a new one
Stashing ongoing form values in the session temporarily lets you access them for as long as the session cookie lasts, so they are not lost if the user quits your form, goes to another site, then comes back to finish. Your form will have to put these back in manually though, so an input might look something like this
<input type="password" name="password" value="<?= isset($_SESSION) && isset($_SESSION['password'] ? $_SESSION['password'] : null; ?>" />
I tried to explain the relevant logic you are not applying rather than just the specific use case, because without actually grasping what the underlying lack of logic is, you would otherwise probably just hit the same wall again. Hopefully this saves you from a number of other bugs that you would otherwise likely hit immediately after this specific thing gets fixed for you.

select tag name not working while using it in php

I have a form in html and I am saving it as search.php:
<form name="myform" action="" method="POST" onsubmit="search_clicked(); return false;">
Keyword<input type="text" name="Keyword" id="Keyword" value="XYZ" required/><!-- value is the default name that appears in the text box-->
<br>
Type
<select name="sel" id="sel" class="form-control" onchange="checkcolors(this.value)">
<option selected value="Users">Users</option>
<option value="Pages">Pages</option>
<option value="Events">Events</option>
<option value="Places">Places</option>
<option value="Groups">Groups</option>
</select>
</form>
<div id="loc_dist_displayarea" style="display:none;">
Location<input type="text" name="Location" value="90007" required/> Distance(meters)<input type="text" name="distance" value="10000" required/>
</div>
<br><br>
<input type="submit" name="Search"/>
<input type="submit" value="clear" id="clear" onclick="return clearclicked()"/>
</form>
and my php script is in the same file:
<div id="body_area" style="display:none">
<?php
echo "hi I am searching ";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
//echo "yes value is selected";
//echo $_POST["Keyword"];
if (isset($_POST['sel'])) {
$selectedval= $_POST["sel"];
echo "$selectedval";
}
//echo $_POST["Location"];
}
echo "no value is selected";
?>
</div>
I am not able to display the $_POST['sel'] while $_POST['Keyword'] is echoed.Please help.
First of all, you arent using good programming practices, you use quotation marks (these " and these ') Indiscriminately. You should only alternate between them when you have them nested.
Next, on the action paramenter you should put the name of the file, even if it's the same.

Detect url in select value and redirect to it

I want to make a form and redirect user to a specific url mentionned in the select value.
This is my form :
<form>
<select name="url" >
<option value="http://www.google.com/">Google</option>
<option value="http://www.bing.com/">Bing</option>
</select>
<input name="name" type="text" placeholder="Name" />
<input name="age" type="text" placeholder="Age" />
<button type="submit">Go</button>
</form>
For example, if the user click on "Go" when "Page2" is selected, he will be redirected to page "http://www.example.com/page2" ("Name" and "Age" values will be transferred to this page).
Just like this code :
<select name="jumpit" onchange="document.location.href=this.value">
<option selected value="">Make a Selection</option>
<option value="http://www.google.com/">Google</option>
<option value="http://www.bing.com">Bing</option>
</select>
except that i need the redirection to be done after the submit button click not onchange select.
That's all. Thanks !
Here you are, also it was tested and working:
HTML
<form action="some_file.php" method="post">
<select name="url">
<option value="http://www.google.com/">Google</option>
<option value="http://www.bing.com/">Bing</option>
</select>
<input name="name" type="text" placeholder="Name" />
<input name="age" type="text" placeholder="Age" />
<button type="submit">Go</button>
</form>
some_file.php
if (isset($_POST['url'])) {
$url = $_POST['url']."?name=".urlencode($_POST['name'])."&age=".urlencode($_POST['age']);
header("Location: ".$url);
exit;
}
Then you can access them with GET, ie. $_GET['name'] will be the same, as you entered into name input field.
Try this code:
<form>
<select name="url" id="url" >
<option value="http://www.example.com/page1">Page1</option>
<option value="http://www.example.com/page2">Page2</option>
<option value="http://www.example.com/page3">Page3</option>
</select>
<input name="name" id="name" type="text" placeholder="Name" />
<input name="age" id="age" type="text" placeholder="Age" />
<input type="button" onClick="submitForm()" />
</form>
<script>
function submitForm()
{
var url = $('#url').val();
var name = $('#name').val();
var age = $('#age').val();
var urlToSend = url + "?name=" + name + "&age="+age;
location.replace(urlToSend);
}
</script>
You can try following code, it will help you :-
Change submit button with these :-
<button type="submit" name="submit">Go</button>
Add following code where your form submit :-
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$age = $_POST['age'];
$url = $_POST['url']."?name=".$name."&age=".$age;
header("Location: ".$url);
}
You can access both value using $_GET in next page.

why am i getting undefined indexs when i use enctype="multipart/form-data"

why am I getting undefined index's with my form is it because of the encoding type I am using, if so what can I do to fix this to properly post my variables
<form enctype="multipart/form-data" name="pmForm" id="pmForm" method="post" action="personalspage.php"><br>
<b>Age</b> <input type="text" name="age" id="age" cols="4"><br><br>
<b>University</b> <select name="university" id="university" onfocus="emptyElement('status')">
<option disabled selected>select one...</option>
<option value="Algoma">Algoma University</option>
<option value="york">York University</option>
</select><br><br>
<b>Headline</b> <input type="text" name="headline" id="headline"><br><br>
<b>Message</b> <textarea name="message" id="message" rows="6" cols="50"></textarea><br><br>
<b>Add a picture</b> <input type="file" name="photo" id="photo" accept="image/*"><br><br>
<input type="hidden" name="mysex" id="mysex" value="<?php echo $_POST["mysex"]; ?>">
<input type="hidden" name="lookingfor" id="lookingfor" value="<?php echo $_POST["lookingfor"]; ?>">
<center><input type="submit" name="adSubmit" id="adSubmit" value="Post It"></center>
</form>
I know that the variables being posted from say page1 to this form are coming through because I have an if statement with an isset() for the variables making it header to another page if there not set. this form code is from page2
im using this code on page3 to recieve the form data
$mysex = $_POST['mysex'];
$lookingfor = $_POST['lookingfor'];
$uni = $_POST['university'];
So when I post all the variable from this form to another page I get
Notice: Undefined index: mysex in C:\xampp\htdocs\Website\personalspage.php on line 4
Notice: Undefined index: lookingfor in C:\xampp\htdocs\Website\personalspage.php on line 5
Notice: Undefined index: university in C:\xampp\htdocs\Website\personalspage.php on line 6
I double checked and made sure that all my methods are using post, the only thing I can think of why this isnt working is because of some sort of combination of echoing input values and the enctype. If anyone could help me out it would be greatly appreciated.
Undefined variable university means you didnt select any options to fix that you can set any of the ptions to selected and for hidden variables problem is value is not there. fixed code is here
<form enctype="multipart/form-data" name="pmForm" id="pmForm" method="post" action="personalspage.php"><br>
<b>Age</b> <input type="text" name="age" id="age" cols="4"><br><br>
<b>University</b> <select name="university" id="university" onfocus="emptyElement('status')">
<option disabled selected value="" selected>select one...</option>
<option value="Algoma">Algoma University</option>
<option value="york">York University</option>
</select><br><br>
<b>Headline</b> <input type="text" name="headline" id="headline"><br><br>
<b>Message</b> <textarea name="message" id="message" rows="6" cols="50"></textarea><br><br>
<b>Add a picture</b> <input type="file" name="photo" id="photo" accept="image/*"><br><br>
<input type="hidden" name="mysex" id="mysex" value="<?php if(isset($_POST["mysex"])) echo $_POST["mysex"];else echo ""; ?>">
<input type="hidden" name="lookingfor" id="lookingfor" value="<?php if(isset($_POST["lookingfor"]))echo $_POST["lookingfor"];else echo ""; ?>">
<center><input type="submit" name="adSubmit" id="adSubmit" value="Post It"></center>
</form>
I Have Any Two option
First:
error_reporting(0);
Second:
<input type="hidden" name="mysex" id="mysex"
value="<?php if(isset($_POST["mysex"])){ echo $_POST["mysex"]; }?>">
<input type="hidden" name="lookingfor" id="lookingfor"
value="<?php if(isset($_POST["lookingfor"])){ echo $_POST["lookingfor"]; }?>">

fieldset and form HTML

I got weird problem over here. I was using in html to send it to my script .php. Everything was fine, script was running like it should. It was looking a little ugly without margins so I tried to edit it a bit. I found a way with some CSS changes and I used . Now it's broken. I don't really know why.
Here's the HTML code:
Before form:
[insert_php]
$current_user = wp_get_current_user();
[/insert_php]
After form
<form action="http://sute.com/rp.php" method="post">
<fieldset><label for="email">Email:</label><input name="email" size="20" type="text" />
<label for="user_name">Nick:</label><input name="user_name" size="20" type="text" />
<label for="server">Server:</label><select name="server">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option></select></fieldset>
<input type="hidden" name="reward" value="123">
<input type="hidden" name="user_id" value="[insert_php]echo "$current_user->ID";[/insert_php]">
<center><input type="submit" value="Submit" /></center></form>
It was working without label for so I don't know why it's not working now, any idea? I'll also give you PHP code here.
$user_id = mysql_real_escape_string($_POST['user_id']);
$user_name = mysql_real_escape_string($_POST['user_name']);
$server = mysql_real_escape_string($_POST['server']);
$email = mysql_real_escape_string($_POST['email']);
$nagroda = mysql_real_escape_string($_POST['reward']);
$price = mysql_real_escape_string($_POST['price']);
echo "<body style='background: url(http://bg.com/bg.jpg) no-repeat fixed center center;'>";
echo "<background-repeat: no-repeat;>";
$pkt = mysql_query("SELECT meta_value FROM wp_usermeta WHERE user_id = $user_id AND meta_key='mycred_default'");
if ( $pkt > 0 OR $user_id == 0) {
$message = 'Sorry, you need more LPoints.';
echo "<SCRIPT type='text/javascript'> //not showing me this
alert('$message');
window.location.replace((\"http://sute.com\"));
</SCRIPT>";
mysql_close();
}
rest of the code, doesn't really matter because it ends here with my problem.
so as I said - it was working before I used fieldsets.
Is this the page your php script is at?
http://sute.com/rp.php
<form action="http://sute.com/rp.php" method="post">
<fieldset>
<label for="email">Email:</label><input name="email" size="20" type="text" />
<label for="user_name">Nick:</label><input name="user_name" size="20" type="text" />
<label for="server">Server:</label>
<select name="server">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</fieldset>
<input type="hidden" name="reward" value="123">
<input type="hidden" name="user_id" value="[insert_php]echo "$current_user->ID";[/insert_php]">
<center><input type="submit" value="Submit" /></center>
</form>

Categories