Not able to print data from Html in Php file - php

<body>
<span>
<div class="heading">
<h3><img src="http://zicom.com/img/ilayashop-1482233381.jpg" alt="Zicom Logo" ></h3>
<h1><i>Zicom </i> SMS Application</h1><br>
</span>
</div>
<br>
<div>
<form method='post' action=''>
<label for="fname">Contact Number</label>
<input type="text" id="fname" name="number" placeholder="Contact Number....">
<label for="sms" id="msg">Message</label>
<select id="country" name="message">
<option value="" name="message">Blank</option>
<option value="hello" name="message">Abandoned Call</option>
<option value="" name="message">Audit Call </option>
</select>
<input type="text"id="msg" name="message" placeholder="Type your Message...." >
<input type="submit" value="Send" name="send">
</form>
</div>
<?php
if(isset($_POST)) {
$n = $_POST['number'];
$m = $_POST['message'];
echo "$n $m";
}
?>
</body>
</html>
I have used the same method many times and i have succesfully sent the data to php file, but im unable to do the same. I try to print the content of form, but is not executing.

Related

Fill HTML5 input field with current date + 14 days

The user is making a coin submission and when they go to do that I want the date to load into a input field that I'm using to track the time the submission was made. That date will be recorded in a mySQL database. The cut off date is set as the coming bimonthly Friday (eg cut off dates are Jun 14 and Jun 28. if the submission is done today, then cut off date is Jun 28. if the submission is done on Jun 30, the cut off date would be Jul 12. I included the entire form so you could get a bigger picture. Feel free to make adjustments. Thanks any help is greatly appreciated.
I'm able to call a javascript function and have the user select the date from a calendar, but that's not what I need. Javascript - get a date from an HTML input but I'm not sure how to concatenate the +14 days for the next cut off date.
CoinSubmission.html
<form action="AdminCoinSub_Code.php" method="POST">
<h1 id="litheader">Coin Submission</h1>
<div class="inset">
<input type="text" list="Store" name="Store" placeholder="Store">
<datalist id="store">
<option value="Causeway Bay">
<option value="Wan Chai">
<option value="Lai Chi Kok">
<option value="Tai Po">
</datalist>
<input type="text" list="Position" name="Position" placeholder="Position">
<datalist id="position">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
</datalist>
<p>
<input type="text" name="Nickame" id="Nickname" placeholder="Nickname">
</p>
<p>
<input type="text" name="Contact" id="Contact" placeholder="Contact">
</p>
<p>
<input type="text" name="MachineCount" id="Machine Count" placeholder="Machine Count">
</p>
<p>
<input type="text" name="CutOffDate" id="CutOffDate" placeholder="Cut Off Date">
</p>
<p>
<input type="text" name="Coins" id="Coins" placeholder="Coins">
</p>
<p>
<input type="file" type="text" name="location" accept="image/*">
<div class="btnConfirm">
<input class="loginLoginValue" type="hidden" name="" value="" />
</div>
</div>
<div class="btnConfirm">
<input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
</div><br><br>
<div class="wrapper2">
<nav>
<ul>
<li>SUBMISSION</li>
<li>OCCUPANCY</li>
<li>ANALYTICS</li>
<li>SEARCH</li>
</ul>
</nav>
</div>
</form>
AdminCoinSub_Code.php
<?php {
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "administrator_logins";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO admincoinsubmission (Store, Position, Nickname, Contact, MachineCount, CutOffDate, Coins, location)
VALUES ('$_POST[Store]','$_POST[Position]','$_POST[Nickame]','$_POST[Contact]','$_POST[MachineCount]','$_POST[CutOffDate]','$_POST[Coins]','$_POST[location]')");
$stmt->bindParam(':Store', $Store);
$stmt->bindParam(':Position', $Position);
$stmt->bindParam(':Nickname', $Nickname);
$stmt->bindParam(':Contact', $Contact);
$stmt->bindParam(':MachineCount', $MachineCount);
$stmt->bindParam(':CutOffDate', $CutOffDate);
$stmt->bindParam(':Coins', $Coins);
$stmt->bindParam(':location', $location);
$stmt->execute();
echo "Success";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
}
?>
When the page loads the date row in the coin submission form should display the cutoffdate.
date = (current date + cutoffdate)
You can achieve the following task using similar code -
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Date +14 days</title>
</head>
<body>
<input type="date" name="date1" id="date1" value="" />
<input type="text" name="date2" id="date2" value="" />
<script type="text/javascript">
var date1 = document.getElementById("date1");
date1.addEventListener('change', function(){
tempDate = new Date(date1.value);
finalDate = tempDate.setDate(tempDate.getDate() + 14);
console.log(new Date(finalDate));
});
</script>
</body>
</html>
To achieve expected result, use below option of add time with getTime() method and add 14 days
var currentDate = new Date(new Date().getTime()+(14*24*3600000))
document.getElementById('CutOffDate').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear()
Working code sample for reference
var currentDate = new Date(new Date().getTime()+(14*24*3600000))
document.getElementById('CutOffDate').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear()
<form action="AdminCoinSub_Code.php" method="POST">
<h1 id="litheader">Coin Submission</h1>
<div class="inset">
<input type="text" list="Store" name="Store" placeholder="Store">
<datalist id="store">
<option value="Causeway Bay">
<option value="Wan Chai">
<option value="Lai Chi Kok">
<option value="Tai Po">
</datalist>
<input type="text" list="Position" name="Position" placeholder="Position">
<datalist id="position">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
</datalist>
<p>
<input type="text" name="Nickame" id="Nickname" placeholder="Nickname">
</p>
<p>
<input type="text" name="Contact" id="Contact" placeholder="Contact">
</p>
<p>
<input type="text" name="MachineCount" id="Machine Count" placeholder="Machine Count">
</p>
<p>
<input type="text" name="CutOffDate" id="CutOffDate" placeholder="Cut Off Date">
</p>
<p>
<input type="text" name="Coins" id="Coins" placeholder="Coins">
</p>
<p>
<input type="file" type="text" name="location" accept="image/*">
<div class="btnConfirm">
<input class="loginLoginValue" type="hidden" name="" value="" />
</div>
</div>
<div class="btnConfirm">
<input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
</div><br><br>
<div class="wrapper2">
<nav>
<ul>
<li>SUBMISSION</li>
<li>OCCUPANCY</li>
<li>ANALYTICS</li>
<li>SEARCH</li>
</ul>
</nav>
</div>
</form>
codepen - https://codepen.io/nagasai/pen/VJWmNE?editors=1010

echo a html page and entering values into a form from php variables

I want to block access to a PHP page.
I'm doing that with this way: If you been logged in, PHP check if exist a cookie, and doing echo the HTML, else it's redirecting you to login page.
Here is the code but when I'm trying to set value attribute equal to a PHP variable, I'm getting back the php code ex.""
The PHP code inside the selection tag, isn't working either!
<?php
if(isset($_COOKIE['User_Email_Cookie'])) {
session_start();
$name =$_SESSION['User_FullName'];
$phone =$_SESSION['User_Phone'];
echo '<!DOCTYPE html>
<html>
<body>
<h1 class="Title">Reserve a table now!</h1>
<center>
<form action="reservation2.php" method="post">
<div class="App">
<div class="User">
<h2 style="text-align:left;"> Contact:</h2>
<input type="text" id="Name" placeholder="Full Name" value="<?php echo $name ?>" required>
<input type="tel" id="Phone" placeholder="Phone" value="<?php echo $phone ?>" required>
</div>
<div class="DatePeople">
<h2> Choose the Date:</h2>
<input type="date" id="Date" name="TableDate">
<select name="Time" class="time">
<option>19:00</option>
<option>19:30</option>
<option>20:00</option>
<option>20:30</option>
<option>21:00 </option>
<option>21:30</option>
<option>22:00</option>
</select>
<h2 style="margin-top:0px;">Choose Table, People: <a target="_blank" href="media/diagram.png"><img src="media/info.png" width="23px"></a></h2>
<select name="TableNum" class="table">
<?php
include \'connectDb.php\'; #Eisagwgi stoixeiwn gia syndesi me ti vasi
$result=mysqli_query($con,"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns WHERE
TABLE_NAME = \'available\' AND COLUMN_NAME NOT IN (\'Date\', \'Time\')");
while($row = mysqli_fetch_array($result)) {
echo \'<option>\'.$row[0].\'</option>\';
}
?>
</select>
<input type="number" id="seats" name="People" min="2" max="8" value="4" >
</div>
</div>
<div>
<input type="submit" name="Submit" value="Reserve">
<a class="button" href="logout.php">Log out</a>
</div> </center>
</form>
else {
header("location: reservation.php");
}
The issue is that you echo the html, and inside that echo you combine "inner" php tags (value="<?php echo $name ?>" instead of value="' . $name . '" for example).
Change:
echo '<!DOCTYPE html>
To:
?><!DOCTYPE html>
And at the end, where you have:
</form>
Replace it with
</form></body></html><?php
The above code allows you combine html markup, by closing the php tags in the correct place, without you having to echo it with php.
Read the documentation for more details.
Please try this code
<?php
if(isset($_COOKIE['User_Email_Cookie'])) {
session_start();
$name =$_SESSION['User_FullName'];
$phone =$_SESSION['User_Phone'];
?>
<!DOCTYPE html>
<html>
<body>
<h1 class="Title">Reserve a table now!</h1>
<center>
<form action="reservation2.php" method="post">
<div class="App">
<div class="User">
<h2 style="text-align:left;"> Contact:</h2>
<input type="text" id="Name" placeholder="Full Name" value="<?php echo $name ?>" required>
<input type="tel" id="Phone" placeholder="Phone" value="<?php echo $phone ?>" required>
</div>
<div class="DatePeople">
<h2> Choose the Date:</h2>
<input type="date" id="Date" name="TableDate">
<select name="Time" class="time">
<option>19:00</option>
<option>19:30</option>
<option>20:00</option>
<option>20:30</option>
<option>21:00 </option>
<option>21:30</option>
<option>22:00</option>
</select>
<h2 style="margin-top:0px;">Choose Table, People: <a target="_blank" href="media/diagram.png"><img src="media/info.png" width="23px"></a></h2>
<select name="TableNum" class="table">
<?php
include \'connectDb.php\'; #Eisagwgi stoixeiwn gia syndesi me ti vasi
$result=mysqli_query($con,"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns WHERE
TABLE_NAME = \'available\' AND COLUMN_NAME NOT IN (\'Date\', \'Time\')");
while($row = mysqli_fetch_array($result)) {
echo \'<option>\'.$row[0].\'</option>\';
}
?>
</select>
<input type="number" id="seats" name="People" min="2" max="8" value="4" >
</div>
</div>
<div>
<input type="submit" name="Submit" value="Reserve">
<a class="button" href="logout.php">Log out</a>
</div> </center>
</form>
<?php
else {
header("location: reservation.php");
}
?>

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.

PHP GET Variable in HTML Form

I'm trying to pre-populate my form values with variables passed through the URL.I've tried many different solutions, sometimes I don't get an error, the variable just doesn't show up. Any help is appreciated, thanks!
URL Example: website.com/?firstname=john
Code:
<html>
<script type="text/javascript">
function writeform(){
selobj = document.getElementById('selform');
newobj = document.getElementById('newform');
p = document.getElementById('menu').selectedIndex + 1;
a = document.getElementById('menu2').selectedIndex + 1;
if((p < 14 && (a == 1 || a == 2 || a == 3 ||a == 4)) { // write the 1st form
txt = 'Person 1: '+'<input type="text"/><br/>';
txt += 'Person 2: '+'<input type="text"/>';
} else {
document.getElementById('div1').style.display='block';
}
// if(p==2 && a==1){ // write the 2nd form
// txt ='Name: '+'<input type="text"/><br/>';
// txt+='Addr: '+'<input type="text"/>';}
newobj.innerHTML=txt;selobj.style.display='block';
}
</script>
<div style="width: 400px; float:left;"> <?php echo $_GET["firstname"]; ?></div>
<div style="width: 400px; float: left;"> <!-- Primary Div-->
<p style="font-size: 16px; font-weight: bold;">Select Something</p>
<div class="fancy3">
<table style="width:350px; height=350px">
<tr>
<td>
<select id="menu" size="14">
<option selected="selected"><b>--- Common Options ---</b></option>
<option></option> //NY
</select>
<br/>
<p style="font-size: 16px; font-weight: bold;">Range</p>
<div class="fancy3">
<table style="width:350px; height=350px">
<tr>
<td>
<select id="menu2" size="4">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<br/>
</td>
<td>
<div id="selform" style="display:none">
<fieldset>
<div id="newform"></div>
</fieldset>
</div>
</td>
</tr>
</table>
</div>
<br/>
<button onclick="writeform();">Search</button></td>
<td>
<div id="selform" style="display:none">
<fieldset>
<div id="newform"></div>
</fieldset>
</div>
</td>
</tr>
</table>
</div>
</div> <!-- Primary Div closing tag-->
<!-- of Field-Specific Forms-->
<div id="div1" style="display:none;">
<form action="http://site1.com/upload" method="get">
First Name: <input name="fname" type="text" value="" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<div id="div1" style="display:none;">
<?php
$firstname = $_GET["firstname"];
?>
<form action="http://site1.com/upload" method="get">
First Name: <input name="fname" type="text" value="<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>
</html>
Test that what you get in $_GET variable by using var_dump($_GET), then use:
echo isset($_GET["firstname"]) ? $_GET["firstname"] : "";
Firstly use print_r($_GET) at the begining of the file to check wether you have the parameters passed.
Then you might want to clean up that mess, because defining $firstname 3 times with the same value just to echo it out makes no sense.
Secondly, you would really like to change those action url as I'm pretty sure it's wrong:
<form action="http://site1.com/upload" method="get">
Thirdly, your input names are name="fname" meanwhile using firstname in $_GET. Not really sure if you will ever relate these two but, whatever.
Some advices:
learn to write code quite more readable than this.
go to jQuery.com and do some research, as it really helps you write less , do more.
CSS doesn't use equal (=) sign as value setter , which in your case is height=350px when it should be height: 350px;.
Give elements some ID's or Classes and use some .css files , it will clean your code more than you can imagine.
You had started wrongly, that's why URL doesn't appering
<script type="text/javascript">
function writeform(){
selobj=document.getElementById('selform');
newobj=document.getElementById('newform');
p=document.getElementById('menu').selectedIndex+1;
a=document.getElementById('menu2').selectedIndex+1;
if((p<14 && (a==1 || a==2 || a==3 ||a==4)){ // write the 1st form
txt ='Person 1: '+'<input type="text"/><br/>';
txt+='Person 2: '+'<input type="text"/>';} else {
document.getElementById('div1').style.display='block';
}
// if(p==2 && a==1){ // write the 2nd form
// txt ='Name: '+'<input type="text"/><br/>';
// txt+='Addr: '+'<input type="text"/>';}
newobj.innerHTML=txt;selobj.style.display='block';}
</script>
<body>
<form action="http://site1.com/upload" method="get">
<?php echo $_GET["firstname"]; ?>
<p style="font-size: 16px; font-weight: bold;">Select Something</p>
<div class="fancy3"><table style="width:350px; height=350px">
<tr><td><select id="menu" size="14">
<option selected="selected"><b>--- Common Options ---</b></option>
<option></option> //NY
</select><br/>
<p style="font-size: 16px; font-weight: bold;">Range</p>
<div class="fancy3"><table style="width:350px; height=350px">
<tr><td><select id="menu2" size="4">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option></select><br/>
</td>
<td><div id="selform" style="display:none">
<fieldset><div id="newform"></div></fieldset></div>
</td></tr></table></div>
<br/>
<button onclick="writeform();">Search</button></td>
<td><div id="selform" style="display:none">
<fieldset><div id="newform"></div></fieldset></div>
</td></tr></table></div>
</div> <!-- Primary Div closing tag-->
<!-- of Field-Specific Forms-->
<div id="div1" style="display:none;">
<form action="http://site1.com/upload" method="get">
First Name: <input name="fname" type="text" value="" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<div id="div1" style="display:none;">
<?php
$firstname = $_GET["firstname"];
?>
First Name: <input name="fname" type="text" value="<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>
</body>
</html>
A couple of problems that I'm seeing here ... for one you have multiple elements with the same id (see id="selform").
To load get variable into a text input the pattern is like this:
<input type='text' name='fieldname' value='<?= isset($_GET['field'])?$_GET['field']:"") ?>'/>
for a checkbox or radio control it is like this
<input type='checkbox' name='fieldname' value='myval' <?= isset($_GET['field']) && $_GET['field'] == 'myval'?"checked=\"checked\"":"") />
for select boxes it you do this:
<select name='fieldname'>
<option value='myval' <?= isset($_GET['field']) && $_GET['field'] == 'myval'?"selected=\"selected\":"" ?>>My Val Label</option>
<option value='myval2' <?= isset($_GET['field']) && $_GET['field'] == 'myval2'?"selected=\"selected\":"" ?>>My Val2 Label</option>
</select>
Here is a nifty select box function that will allow you too more concisely output a select in your code (i find the check with every element a little tedious)
function showSelect($name, $options, $selected, $attr = array()){
$str = "<select name='".$name.'"';
foreach($attr as $name=>$val){
$str.= " ".$name."='".$val."'";
}
$str.=">";
foreach($options as $k=>$val){
$str.= "<option value='".$val."'".($val==$selected?" selected='selected'":"").">".$k.'</option>';
}
$str.="</select>";
}
and you can use it like this...
$days = array();
for($d = 1; $x<=31; $x++){
$days[(string)$d] = (string)$d;
}
echo showSelect("formDays", $days, $_POST["formDays"], array("id"=>"formDays"))

How to make multiple-form with single submit button and action with php

Rather inputing one by one for a class/lessen for a day, I'd like to input 10 forms at once.
The HTML is something like this.
All the form do the same thing, add start_time, finish_time and instructor in a database.
However I am not sure how to do this. And I am not sure if this HTML is correct or not.
Any inputs will be appreciated.
Thanks in advance.
HTML
<?php
$date = "2010-10-08";
?>
<form name="form1" method="post" action="inputform.php">
<!-- form 1 -->
<label for='start_time'>1. Start Time</label>
<input type="text" name="start_time" />
<label for='finish_time'>Finish Time</label>
<input type="text" name="finish_time" />
<label for='instructor'>Instructor</label>
<select name="instructor">
<option value="john">John</option>
<option value="mary">Mary</option>
<option value="jim">Jim</option>
</select>
<input type="hidden" name="date" value="<?php echo $date; ?>"/>
<div style="clear: both;"> </div>
<!-- form 2 -->
<label for='start_time'>2. Start Time</label>
<input type="text" name="start_time" />
<label for='finish_time'>Finish Time</label>
<input type="text" name="finish_time" />
<label for='instructor'>Instructor</label>
<select name="instructor">
<option value="john">John</option>
<option value="mary">Mary</option>
<option value="jim">Jim</option>
</select>
<input type="hidden" name="date" value="<?php echo $date; ?>"/>
<div style="clear: both;"> </div>
<!-- form 3 -->
<label for='start_time'>3. Start Time</label>
<input type="text" name="start_time" />
<label for='finish_time'>Finish Time</label>
<input type="text" name="finish_time" />
<label for='instructor'>Instructor</label>
<select name="instructor">
<option value="john">John</option>
<option value="mary">Mary</option>
<option value="jim">Jim</option>
</select>
<input type="hidden" name="date" value="<?php echo $date; ?>"/>
<div style="clear: both;"> </div>
<!-- form 4,5,6,7,8,9,10 -->
<input type="submit" name="submit" value="Submit" />
</form>
you cannot submit multiple forms at once.
only one form with the form specific method/action.
however what probably need are arrays.
you can do something like
<form ...>
<select name="instructor[]">
...
</select>
<select name="instructor[]">
...
</select>
then you will get an array posted which you can loop through.
just look at the structure by printing out $_POST like print_r($_POST); then you'll see

Categories