html and php form field and submission - 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 } ?>

Related

How to maintain the PHP GET value on the address bar if I use another GET?

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>

Problem getting PHP session variable in next pahe if it is assigned by HTML select Tag

I need help in transferring the session variable from one page to another while choosing the session variable from the SELECT tag. I have given my code below.
I want to show the selected option in the page1.php and when the option is selected and after that when user clicks or submit the form then the value of session variable transferred to page2.php.
If I don’t redirect my page to page2.php then after clicking the submit button I can get the value of the selected session option on the same page. However, for this to achieve I had the click submit. So I found another solution and with jQuery, I got the selected value in page1.php one option is selected without submitting the form.
Now when I redirect to page2.php then my session variable does not change according to the selected value. Whatever value show it first remain unchanged.
I am new, it may be a silly question, but I am not able to fix it. I appreciate if I can get help/hint
to fix this issue, thank
page1.php:
<?PHP
session_start();
if (isset($_POST['submit'])) {
$city = $_POST['city'];
$_SESSION['city'] = $city;
}
?>
<div><?php echo $_SESSION['city']; ?></div>
<form method="post" action="page2.php">
<select name="city" id="city>
<option value="" selected>Please select city</option>
<option value="London">London</option>
<option value="Sydney">Sydney</option>
</select>
<input type="submit" name="submit" id="submit" value="Next">
<br>
<p id="city"></p>
</form>
*************************
<script>
$('select[name="city"]').on('change', function () {
var option = $('select[name="city"]:checked');
if (this.value == 'London') {
$("#city").html("London");
} else if (this.value == 'Sydney') {
$("#city").html("Sydney");
}
});
</script>
page2.php:
<?PHP
session_start();
?>
<div><?php echo $_SESSION['city']; ?></div>
Based on the method of using jQuery here, i would suggest the following.
Rather than storing selected drop down value in the <p id="city"></p> element.
Define an input tag
<input type="text" id="city" name="city" value=""/>
And using the jQuery store the value in this element.
<?PHP
session_start();
if (isset($_POST['submit'])) {
$city = $_POST['city'];
$_SESSION['city'] = $city;
}
?>
<div><?php echo $_SESSION['city']; ?></div>
<form method="post" action="page2.php">
<select name="city" id="city>
<option value="" selected>Please select city</option>
<option value="London">London</option>
<option value="Sydney">Sydney</option>
</select>
<input type="submit" name="submit" id="submit" value="Next">
<br>
<input type="text" id="city" name="city" value=""/>
</form>
*************************
<script>
$('select[name="city"]').on('change', function () {
var option = $('select[name="city"]:checked');
if (this.value == 'London') {
$("#city").val("London");
} else if (this.value == 'Sydney') {
$("#city").val("Sydney");
}
});
</script>
************************
************************
page2.php
<?PHP
session_start();
?>
<div><?php echo $_SESSION['city']; ?></div>
Hope you find this useful.

How to do client form data validation in PHP/HTML

I have three php files. One is the controller "index.php". One is for displaying output "people.php". The other is a form for user input "form.php"
form.php looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<form action = "" method = "post">
First name: <input name ="first_name" type = "test" size = "55" maxlength = "55"><p>
Second name: <input name ="second_name" type = "text" size = "55" maxlength = "55"><p>
Month: <select name="month">
<option value="">select month</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option><p>
<option value="April">April</option><p>
<option value="May">May</option><p>
<option value="June">June</option><p>
<option value="July">July</option><p>
<option value="August">August</option><p>
<option value="September">September</option><p>
<option value="October">October</option><p>
<option value="November">November</option><p>
<option value="December">December</option><p>
</select><p>
Year: <select name ="year">
<option value = ""> -select year-</option>
<option value = "2015"> 2015 </option>
<option value = "2016"> 2016 </option>
<option value = "2017"> 2017 </option>
</select><p>
Date of birth:<input name="date_of_birth" input type="text" id="datepicker"></p>
<input name = "add_person" type="submit" value = "submit">
</form>
</html>
Index.php looks like this:
<?php
if (!isset($_POST['add_person'])){
include 'form.php';
}
elseif (isset($_POST['add_person']) && empty($_POST['first_name']) && empty($_POST['second_name']) && empty($_POST['month']) && empty($_POST['year']) && empty($_POST['date_of_birth'])){
include 'form.php';
}
elseif (isset($_POST['add_person'])){
if (empty($_POST['first_name'])){
exit("First name cannot be left empty");
}
if(empty($_POST['second_name'])){
exit("Second name cannot be left empty");
}
if(empty($_POST['month'])){
exit("month cannot be left empty");
}
if(empty($_POST['year'])){
exit("year cannot be left empty");
}
if(empty($_POST['date_of_birth'])){
exit("date of birth cannot be left empty");
}
$date1 = $_POST['date_of_birth'];
$date2 = date("Y-m-d");
if($date2 < $date1){
exit("Invalid date: Date of birth cannot be in the future");
}
$authors = $_POST['first_name'];
$title = $_POST['second_name'];
$month = $_POST['month'];
$year = $_POST['year'];
echo "$first_name <br> $second_name <br> $month <br> $year <br> $date1";
?>
}
else{
include "form.php";
}
?>
and people.php looks like this:
<html>
<head>
<title></title>
<script>
<!--jquery imebidi(necessary evil)....jquery copy-pasted from here https://jqueryui.com/datepicker/ -->
$(function() {
var date = $('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
});
</script>
</head>
<body>
<div id="header">
<div class ="center">
<p>Manuscript management information system</p>
</div>
</div>
<div id="content-wrapper">
<div id="content-main">
<p></p>
<?php
include 'index.php';
?>
<p>
</div>
<div id="content-secondary">
<p>mmmmmmmmmmmmm</p>
</div>
</div>
<div id="sidebar">
<li>Recently published</li>
<li>Submitted to PubCom</li>
<li></li>
</div>
<div id="footer"><p>Welcome</p>
</div>
</body>
</html>
What I'm trying to achieve is to have the error message displayed on the form without having to be transferred to a new page. I mean, if a user fails to enter first name then he/she should get an error on the same form.
You can either add a "required" attribute to every required field :
First name: <input name ="first_name" type = "test" size = "55" maxlength = "55" required>
Or add a "onsubmit" attribute to your form and write a validation scipt in javascript :
<form action = "" method = "post" onsubmit="return validation();">
<script>
function validation(){
if(<conditions>){
return true;
}else{
return false;
}
</script>
you can use HTML 5 Form validation rules . Use the required attribute for getting non empty fields.
<input type="text" name="name" required>
you can also use various input types email, url, number, tel, date to validate specific types . refer this link for more details .
<input type="email" name="email" required placeholder="Enter a valid email address">
other options is to use javascript . check this stackoverflow thread on how to achieve this using jquery .

display the output in the same page using php

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"/>
^

Confused why form is not submitting

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.

Categories