im new to php.i created a page in php containing chk boxes ,input fields and retrive the data from the database(mysql) and also use the javascript.
i h've a problem in this ,that is i want to display the output in the same page.
i created the code like this,i want the output in same page,but it shows error.
<?php
if(isset($_POST['submit']))
{
$con=mysql_connect("localhost","demosdef_review","review123");
if(!$con)
{
die('could nt connect 2 the server');
}
mysql_select_db("demosdef_review", $con);
$state = $_POST["state"];
$country = $_POST['country'];
$contry = "USA";
if($country==1)
{
$result = mysql_query("SELECT address FROM storelocator WHERE countryid='1' AND id='$state'");
$row = mysql_fetch_array($result);
echo $row['address'];
}
else
{
$result = mysql_query("SELECT address FROM storelocator WHERE countryid='$country'");
$row = mysql_fetch_array($result);
echo $row['address'];
}
}
?>
enter code here
<div class=buy>
Dealer Enquiry
</div><br/>
<div class=buyfont>Authorized Retailers</div>
<div><img src="images/archivers.png"><br/>
<a href="http://www.archiversannex.com/Books-And-SoftwareSoftware-And-Accessoriesdefault.aspx?PageID=20&CategoryID=48"/>
<img src="images/logo_archivers_annex.png" ></a><br/><br/>
<br/><br/>
</div><br/>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>
<p>
<label for="country" style="padding-right: 2px;">Country</label><select name="country" value="countryid"
id="countryid"
onchange="sub()" style="width:120px;">
<option value="1">USA</option>
<option value="2">CANADA</option>
<option value="3">UK</option>
<option value="4">AUSTRALIA</option>
<option value="5">ITALY</option>
<option value="6">GUATEMALA</option>
<option value="7">NEW ZEALAND</option></select></p>
<p>
<label for="state" style="padding-right: 14px;">State</label>
<select id="state" name="state" value="state" style="width:120px">
<option value="7">Alabama</option>
<option value="8">Alaska</option>
<option value="9">Arizona</option>
<option value="10">Arkansas</option>
<option value="11">California</option>
<option value="12">Colorado</option>
<option value="13">Connecticut</option>
<option value="43">Florida</option>
<option value="14">Georgia</option>
<option value="15">Idaho</option>
<option value="16">Illinois</option>
</select></p> <br/>
<input class="dealer-submit" type="submit" value="Submit" onClick="buy_func()"/>
</form>
<script type="text/javascript">
function sub()
{
var x=document.getElementById("countryid").selectedIndex;
var y=document.getElementById("countryid").options;
var z = document.forms[0].state;
if(y[x].index==0){
z.disabled = false;}
else if(y[x].index>0) {
z.disabled = true;}}
</script>
Your HTML markup has an error
<a href="http://www.hobbylobby.com/storelocator"/>
It should be
<a href="http://www.hobbylobby.com/storelocator">
Also try
<form method="POST" action="">
Keeping the action blank will post it to the same page itself
And replace your submit button with this
<input class="dealer-submit" type="submit" name="submit" value="Submit" onClick="buy_func()"/>
Check the value of the submit button, if its set isset($_GET('submit')) then display the results
does that solve your problem?
submit is the type of the element, 'Submit' is the value you are looking for:
<?php
if(isset($_POST['Submit']){
}
?>
Remove the backslash at the end of the line:
<a href="http://www.hobbylobby.com/storelocator"/>
^
Related
I am passing a variable from page1 to page2 using GET and it's already working. However, I also use another get in the <select> <option> on page2 which means when I click an item in the option, the value I get from page1 disappears and becomes undefined since the address bar changes. I want the variable I get from page1 to be compared on SQL where clause. I can't think of a way on how to solve this, I'm not really knowledgeable in PHP, can you help me?
Here is my code(I remove some unnecessary lines): page1
<form method="get" action="home.php">
<div class="form-group">
<input id="codehe" class="form-control" type="codehe" name="codehe" placeholder="Class Code">
</div>
<div class="form-group">
<input class="form-control button" type="submit">
</div>
</form>
page2 (this is how i get the value from page1)
<?php
$codehe = $_POST['codehe'];
echo $codehe;
?>
The other GET (form) on page2
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
</form>
<?php
if(isset($_GET['state'])) {
$str = $_GET["state"];
$sql = "SELECT * FROM classvideo WHERE subjects = '$str' AND linkcode = ''";
?>
The traditional way to do things like this (without client-side code) is to put a hidden input in your form on page 2:
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
<!-- Add this line -->
<input type="hidden" name="codehe" value="<?php echo $_GET['codehe'] %>">
</form>
I want send the data I received from the select tag through the url as queries so I can use it and query the database.
I have tried but the values are not showing. I am getting something like: type=&action=
This is my code
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
$type = $_POST['types'];
$action = $_POST['action'];
$query = "type={$type}&action={$action}";
<a class="site-btn fs-submit" href="search.php?<?php echo $query; ?>"> Advanced search</a>
I expected :
type=2 bed room&action=rent
Try the following code. Here, When you submit the form the whole form data will be passed to search.php and the form method is POST so you can access those data by simply calling $_POST['attribute value of name'].
HTML form:
<form class="filter-form" method="post" action="search.php" enctype="multipart/form-data">
<input type="text "class="d-block d-md-inline" placeholder="Enter State, City or Area" name="city">
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
<input type="submit" value="Advanced search">
</form>
create a PHP file (search.php):
<?php
//Do what you want here
if (isset($_POST['city'])) {
$city = $_POST['city'];
echo $city;
}
if (isset($_POST['types'])) {
$type = $_POST['types'];
echo $type;
}
if (isset($_POST['action'])) {
$action = $_POST['action'];
echo $action;
}
?>
Replace the link with a button. Attach the query params in the link to the action of the form. Your code should look like below.
<form action="search.php?<?php echo $query; ?>">
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
<?php
$type = $_POST['types'];
$action = $_POST['action'];
$query = "type={$type}&action={$action}";
?>
<button class="site-btn fs-submit" type="submit">Submit</button>
</form>
I have a registration form where I want to be able to get the account plan users have selected form another page and will autofill my form for account type.
The a href from the pricing site
signup.php?type=free
signup.php?type=pro
signup.php?type=plus
Code for the form in registration site
<form class="form-signin" action="pricing.php" method="get">
<div class="form-group">
<label for="type">Account Type</label>
<select class="form-control" id="type">
<option value="free">Free</option>
<option value="pro">Pro</option>
<option value="plus">Plus</option>
</select>
</div></form>
This will get the $_GET['type'] variable & set selected attribute for the drop down! You could also do it with jQuery (seen below)
<?php
if(isset($_GET['type'])){
$type = $_GET['type'];
}
?>
<form class="form-signin" action="pricing.php" method="get">
<div class="form-group">
<label for="type">Account Type</label>
<select class="form-control" id="type">
<option <?php if($type == 'free'){ echo "selected='selected'"; } ?> value="free">Free</option>
<option <?php if($type == 'pro'){ echo "selected='selected'"; } ?> value="pro">Pro</option>
<option <?php if($type == 'plus'){ echo "selected='selected'"; } ?> value="plus">Plus</option>
</select>
</div></form>
jQuery...
<?php if(isset($_GET['type'])){ ?>
<script>
$(document).ready(function(){
var type = "<?php echo $_GET['type'] ?>";
$("#type > option").each(function(){
if($(this).val() == type){
$(this).attr("selected", "selected")
}
})
})
</script>
<?php } ?>
I have a short html form and some php code on the same page so that when the form is submitted, the entries appear on the same page. This code works as far as posting the entered information from the form to the page, but I have 2 problems:
The text box for some reason was only letting me enter 1 character, now it won't let me enter any characters.
Every time I refresh the page to try the form again, the information keeps appending. I only want/need for it to show up once after submission.
<form method="post" action="">
<label>Select Out of Office Message
<select name = "selectoutofofficemessage">
<option value = "N/A">N/A</option>
<option value = "Vacation">Vacation</option>
<option value = "Conference">Conference</option>
<option value = "Meeting">Meeting</option>
<option value = "Other">Other</option>
</select>
<label>Custom Out of Office Message
<input type="text" name="customoutofofficemessage" size="30" maxlength="255"/>
</label>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<?php
$selectoutofofficemessage = $_POST["selectoutofofficemessage"];
$customoutofofficemessage = $_POST["customoutofofficemessage"];
$posts = file_get_contents("posts.txt");
$posts = "$selectoutofofficemessage - $customoutofofficemessage\n" . $posts;
file_put_contents("posts.txt", $posts);
echo $posts;
?>
Put a validation to the Post values
if(isset(selectoutofofficemessage))
{
execute the html code;
}
else
{
php code....
} then the value will not be repeated again and again and dont append it just display it....
1.First label is incorrect, it doesn't close. Fix it:
<label>Select Out of Office Message
<select>
...
</select>
</label>
2.Change your PHP code:
<?php
$posts = file_exists("posts.txt") ? file_get_contents("posts.txt") : "";
if(!empty($_POST))
{
$selectoutofofficemessage = $_POST["selectoutofofficemessage"];
$customoutofofficemessage = $_POST["customoutofofficemessage"];
$posts = "$selectoutofofficemessage - $customoutofofficemessage\n" . $posts;
file_put_contents("posts.txt", $posts);
}
echo $posts;
Consider each page refresh after a submition causes the last submition.
<form method="post" action="">
<label>Select Out of Office Message</label>
<select name = "selectoutofofficemessage">
<option selected="selected" value="">Select a Message</option>
<option value ="N/A">N/A</option>
<option value = "Vacation">Vacation</option>
<option value = "Conference">Conference</option>
<option value = "Meeting">Meeting</option>
<option value = "Other">Other</option>
</select>
<label>Custom Out of Office Message</label>
<input type="text" name="customoutofofficemessage" size="30" maxlength="255"/>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<?php
$posts = file_exists("posts.txt") ? file_get_contents("posts.txt") : "";
if(!empty($_POST))
{
$selectoutofofficemessage = $_POST["selectoutofofficemessage"];
$customoutofofficemessage = $_POST["customoutofofficemessage"];
$posts = "$selectoutofofficemessage - $customoutofofficemessage AT <small><em>". date('h:m A - M-d-Y') ."</em></small> <br>" . $posts;
file_put_contents("posts.txt", $posts);
}
echo $posts;
?>
<?php
if (!empty($_POST)){
?>
<script type="text/javascript">
window.location = window.location.href;
</script>
<?php } ?>
I don't think there's anything I left out, but the form doesn't seem to be transmitting any data upon hitting submit. My code is as follows, I realize it's kind of long, but I wanted to include the entire form. All of the attached functions just check if the input was valid:
<form name="formname" id="formname" action="database.php" method = post onsubmit="return checker()">
Username: <input type='text' id='un' onchange="checkname(id)" onkeyup="checkempty(id)" ><div id="un1"></div><br>
Reporter Title:<br>
<select id="s2" onblur="checktype(id)" >
<option value="choose">Choose one</option>
<option value="Assistant Director">Assistant Director</option>
<option value="Director">Director</option>
<option value="DB Admin">DB Admin</option>
<option value="Systems Admin">Systems Admin</option>
</select><div id="s21"></div>
<br>
Password: <input type='password' id='pw' onchange="checkpass(id)" onkeyup="checkempty(id)"><div id="pw1"></div><br>
Email: <input type='text' id='eml'onchange="checkemail(id)" onkeyup="checkempty(id)"><div id="eml1"></div><br>
Description:<br> <textarea rows="6" cols="20" id="desc" onchange="checkdesc(id)" onkeyup="checkempty(id)" ></textarea><div id="desc1"></div><br>
Type of Incident:<br>
<select id="s1" onblur="checktype(id)" >
<option value="choose">Choose one</option>
<option value="afs">afs</option>
<option value="password">password</option>
<option value="hardware">hardware</option>
<option value="other">other</option>
</select><div id="s11"></div>
<br>
<?php
include("connect.php");
$ret = mysql_query("SELECT * FROM countries");
echo "Choose your location:<br>";
echo "<select id='countries' onblur='checktype(id)'>";
echo "<option value='choose'>Choose one</option>";
while($array = mysql_fetch_array($ret)){
echo "<option value='{$array['abbrevs']}'>{$array['full']}</option>";
}
echo "</select><br>";
?>
<div id="countries1"></div><br>
<p>
Would you like an email copy?
<select id="s3">
<option value="no">No</option>
<option value="yes">Yes</option>
</select>
<input type="submit" id = "sub" value= "Submit">
</p>
</form>
and the php I tried to receive it with
<?php
include("connect.php");
$username = $_GET['un'];
$password = $_GET['s2'];
$reporter = $_GET['pw'];
$email = $_GET['eml'];
$description = $_GET['desc'];
$type = $_GET['s1'];
$country = $_GET['countries'];
$emailopt = $_GET['s3'];
?>
the checker function:
function checker(){
if(isgood && isgood1 && isgood2 && isgood3 && isgood4 && isgood5 && isgood6)
{
return true;
}
else{
document.getElementById('subb').style.visibility="visible";
document.getElementById('subb').innerHTML = "You must fully complete the form.";
return false;
}
where the "isgoods" where just quick flags I made for validations, which was all working properly.
Your form's method is POST so you should use $_POST instead of $_GET in your PHP script.
Also you should fix up your HTML so it's valid, you need to quote the method attribute properly:
<form name="formname" id="formname" action="database.php" method="post" onsubmit="return checker()">
Well, I feel stupid. The problem was that I didn't give my HTML elements names, but rather just IDs. I was so used to using ajax and submitting by just getting the value with javascript via the ID that I forgot about names O.o
You are submitting your form with method="POST", so in your PHP you need to read the POST values:
$username = $_POST['un'];
...
Had you submitted your form with method="GET", your PHP code would work. Read about the difference here.