How to get exact time upon button click? - php

I have this working code yet upon button click, it populate the textbox with the timestamp when the page was reloaded/loaded; What I like is for the button to populate the textbox with the time upon click. Anything that can help me will be greatly appreciated, Cheers!
Code:
<html>
<body>
<?php
echo date("Y/m/d");
echo "<br>";
$time= date("h:i:s A", strtotime("now"-8));
echo "<input type=textfield name=time value='$time' size=9>";
echo "<br>";
echo "<input type=\"textfield\" id=\"textbox\" value='' size=9>";
echo "<input type=\"button\" value=\"Time\" onClick=\"document.getElementById ('textbox').value='$time'\">";
?>
<br>
<input id="date" name="curTime" value="<?php echo date('M d,Y'); ?>" size="9" maxlength="10" Required />
</body>
</html>

If you want to get the actual time when the button is clicked, you will have to do that on the client side with JavaScript.
For example,
<!DOCTYPE html>
<html>
<head>
<title>Button Click</title>
</head>
<body>
<input type="text" id="date" value="" />
<input id="button" type="button" value="Get the Date!" onclick="document.getElementById('date').value = new Date().toLocaleTimeString(navigator.language, {hour: '2-digit', minute:'2-digit'})" />
</body>
</html>
This will populate the textarea with the current date upon click. You can send that back to your server through AJAX, POST, etc.
Update
Since you said you were using IE 8 and it looks like IE 8 doesn't support formatting the toLocaleString() output, you could filter offer the seconds with a regular expression. I would highly recommend that you just not support IE 8 and not do the following unless you really have to.
<!DOCTYPE html>
<html>
<head>
<title>Button Click</title>
<script type="text/javascript">
function filter_seconds(date){
return date.replace(/(\d{1,2}\:\d{1,2})\:\d{2}/, '$1')
}
</script>
</head>
<body>
<input type="text" id="date" value="" />
<input id="button" type="button" value="Get the Date!" onclick="document.getElementById('date').value = filter_seconds(new Date().toLocaleTimeString(navigator.language))" />
</body>
</html>

Related

Calculating total in PHP

I have my form done but I am trying to create a handling form which calculates the total plus shipping of $5 and 8% tax rate and displays it.
Need a handling form for my following PHP code. The handling form should calculate total price, 8%tax, $5 shipping fee. It should say thank you for ordering (name entered in form) on (date entered in form)
Need a handling form for my following PHP code. The handling form should calculate total price, 8%tax, $5 shipping fee. It should say thank you for ordering (name entered in form) on (date entered in form)
<html
<head>
</head>
<body>
<link rel= "stylesheet" href= "order.css">
<form action="complete.php" method="post">
<form name="order">
<fieldset><legend>Complete Order:</legend>
<h1> Choose Design </h1>
<p><label>Your Name: <input type="text" name="name"></label>
<label>Address: <input type="text" name="address"></label>
<label>Credit Card #: <input type="text" name="creditcard"></label>
<label>Date: <input type="date" id="datepicker" name='date' size='9' value="" > </label>
<br><label> Design Types: <img src="1.jpg"><input type="checkbox" name="1"></label> $1.00
<label><img src="2.jpg"><input type="checkbox" name="2"> </label>$1.00
<label><img src="3.jpg"><input type="checkbox" name="3"> </label>$1.00
<br></p>
<input type="submit" value="Submit Order">
</form>
</body>
</html>
This is the handler, but before I start on the answer. I just wish to make it clear that Stack Overflow is a Q&A environment and not a place for someone to do your work. That being said, I have answered this question...
Place this code at the top of the HTML page that I will post (I have made some amendments).
Note the $date variable uses the ternary operator to ensure a date will get posted to the database. I have formatted the date in the yyyy-mm-dd format which is the format of MySQL databases (I'm not sure about the format the others use, but I imagine it is the same).
<?php
// only run if form is posted
if (isset($_POST["submit"]))
{
// set POST to variables (easy to type variable name)
$name = $_POST["name"];
$address = $_POST["address"];
$creditcard = $_POST["creditcard"];
$date = $_POST["date"] == "" ? date("Y-m-d") : $_POST["date"];
$design = $_POST["designType"];
$total = $design; // add the cost of design
$total += $total * 1.08; // work out tax
$total += 5; // the shipping fee
}
?>
I have made a few changes to the form that you posted. Here they are:
I indented the tags (it's a personal preference but I think it makes it look neater)
Added </fieldset> to the end of the form. This ensures it passes W3C Validator which requires closing of tags
I changed the checkboxes to radio types. This was an assumption based on how I thought you form was supposed to be (I wasn't sure where total was coming from otherwise; so I assumed the "Design type" was the price)
I reformatted the <label> and <input> so that they weren't nested (with the exception of radio elements, but that is so you can click the label to select the radio)
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
<link rel="stylesheet" href="order.css" />
</head>
<body>
<?php if (isset($_POST["submit"])) { ?><div id="message">Thank you, <?php echo $name; ?> for ordering on <?php echo $date; ?>. The total was <?php echo "$" . number_format($total, 2); ?> </div><?php } ?>
<form name="order" action="complete.php" method="post">
<fieldset>
<legend>Complete Order:</legend>
<h1>Choose Design</h1>
<label>Your Name:</label><input type="text" name="name" />
<label>Address:</label><input type="text" name="address" />
<label>Credit Card #:</label><input type="text" name="creditcard" />
<label>Date:</label><input type="date" id="datepicker" name='date' size='9' value="" />
<br />
<label>Design Types:</label>
<label><img src="1.jpg" alt="$1.00" /><input type="radio" name="designType" value="1" />$1.00</label>
<label><img src="2.jpg" alt="$2.00" /><input type="radio" name="designType" value="2" />$2.00</label>
<label><img src="3.jpg" alt="$3.00" /><input type="radio" name="designType" value="3" />$3.00</label>
<br />
<input type="submit" name="submit" value="Submit Order" />
</fieldset>
</form>
</body>
</html>
That is all.
Again, let me reinforce the idea that Stack Overflow is not here for this type of "question". Please also provide an attempt at your problem as well.

Form submit is not sending any POST data

On my web page, when I press the "Add Customer" link, the following happens:
the onclick handler is called, which
sets values into the forms two text fields
displays an alert (at this point you can see the new values in the text fields on the screen)
calls the form's submit button (Note: the form submits back to it's own PHP file)
The same php file is called, but there are no POST values received
I've verified this with
the code in the first line that counts the values in $_POST then displays later
using fiddler to look at the request sent to the page
I've done this type of thing numerous times before with no problems. I've copied this code to two different web servers (linux, apache) with the same result. It's probably a minor problem but I can't find it.
I've stripped out a whole bunch of code to get down to this little bit, but haven't figured out why no POST values are being sent.
You can see a working copy of this at http://www.daleann.org/dla.php. The only thing need besides the code below is /js/jquery.min.js.
Thanks for your help.
<?php
$pc=count($_POST)."<br />".date("H:i:s");
?>
<html>
<head>
<script src="/js/jquery.min.js" /></script>
<script type="text/javascript">
$(document).ready(function() {
alert("inside docReady");
$(document).on('click', "a.menuBillingOwner", function() {
$("#selectedBillingOwner").val("11");
$("#lastCustomNavSelected").val("selectedBillingOwner");
alert("selectedBillingOwner = "+document.forms['formBillingOwner'].elements['selectedBillingOwner'].value);
document.forms['formBillingOwner'].submit();
});
});
</script>
</head>
<body>
<ul id="menuBillingOwner">
<li><a href='#' id='menuBillingOwnerAdd' class='menuBillingOwner'>Add Customer</a></li>
</ul>
<?php
$lastCustomNavSelected = $selectedBillingOwner = "";
if (count($_POST) > 0 && isset($_POST['selectedBillingOwner'])) {
$lastCustomNavSelected = "selectedBillingOwner";
$selectedBillingOwner = $_POST['selectedBillingOwner'];
}
?>
<?php echo "pc = ".$pc."<br />\n"; ?>
<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
<input type="text" id="lastCustomNavSelected" value="<?php echo $lastCustomNavSelected; ?>" />
<input type="text" id="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
</form>
</body>
</html>
Simple, because your form fields don't have name attributes, see below
<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
<input type="text" id="lastCustomNavSelected" value="<?php echo $lastCustomNavSelected; ?>" />
<input type="text" id="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
</form>
Add names to them, for example:
<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
<input type="text" id="lastCustomNavSelected" name="lastCustomNavSelected" value="<?php echo $lastCustomNavSelected; ?>" />
<input type="text" id="selectedBillingOwner" name="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
</form>
Note: Probably your jQuery assignments need to be fixed too but if that was the only issue then atleast a wrong value should have been POSTed to PHP, hence that is not the issue.
Delete these 2 lines of your jQuery.
$("#selectedBillingOwner").val("11");
$("#lastCustomNavSelected").val("selectedBillingOwner");
because that will change the value of textfield before your submit the form

Adding numbers using HTML forms and PHP

I'm trying to make a simple web application that adds scores from a physical game such as Scrabble. Most of the code is a HTML form, asking for one input per form element. It then puts the data generated from the form and initializes the appropriate variables. The one part I can't figure out is how to add the new score to the last score. I tried to add variables, like $lastScore, but that didn't seem to work either. Does anyone have any suggestions?
<?php
//Gets data from HTML form
$addScore1 = $_REQUEST['addScore1'];
$addScore2 = $_REQUEST['addScore2'];
//Generates HTML form
echo "<!DOCTYPE html>
<html>
<head>
<title>Score Add</title>
</head>
<body>
<div id=\"displayNames\">
<p>
$player1
<form method=\"post\" action=\"\">
<label for=\"addScore1\">Enter your score:</label>
<input type=\"text\" name=\"addScore1\" id=\"addScore1\" />
<input type=\"submit\" />
</form>
</p>
<p>
$player2
<form method=\"post\" action=\"\">
<label for=\"addScore2\">Enter your score:</label>
<input type=\"text\" name=\"addScore2\" id=\"addScore2\" />
<input type=\"submit\"/>
</form>
</p>
</div>
</body>
</html>";
?>
On submit, the script is calling itself and loses all the variables. You need to store their current score, e.g. in the session, in a database, or in a hidden field in the form.
With hidden field in your form:
<form method="post" action="">
<label for="addScore1">Enter your score:</label>
<input type="text" name="addScore1" id="addScore1" />
<--! the addition is done in the next line in value-->
<input type="hidden" name="oldScore1" id="oldScore1" value="<?=($oldscore1 + $addScore1)?>" />
<input type="submit" />
</form>
Do the same with your second form --> oldScore2 etc...
At the top of your script, read the oldscore from $_REQUEST
//Gets data from HTML form
$addScore1 = $_REQUEST['addScore1'];
$addScore2 = $_REQUEST['addScore2'];
$oldScore1 = $_REQUEST['oldScore1'];
$oldScore2 = $_REQUEST['oldScore2'];
// as alternative, do the addition here:
$oldScore1 += $addScore1;
$oldScore2 += $addScore2;
Rewriting your code as a whole, try it:
I edited all those <?php=$somevalue?>because they don't seem to work with your PHP-setup, and replaced them with <?php echo $somevalue> ?>... let me know how it works...
<?php
// Get data from HTML form. $_POST is fine, because form method is set to POST.
$addScore1 = $_POST['addScore1'];
$addScore2 = $_POST['addScore2'];
$oldScore1 = $_POST['oldScore1'];
$oldScore2 = $_POST['oldScore2'];
// if these are numeric values, add them up
if (is_numeric($addScore1) && is_numeric($oldScore1)) $oldScore1 += $addScore1;
if (is_numeric($addScore2) && is_numeric($oldScore2)) $oldScore2 += $addScore2;
// Generate HTML form -- in HTML, much to complicated in PHP, unless it is necessary for sth else
?>
<html>
<head>
<title>Score Add</title>
</head>
<body>
<div id="displayNames">
<p><?php echo $player1; ?> current score: <?php echo $oldScore1; ?>
<form method="post" action="">
<label for="addScore1">Enter your score:</label>
<input type="text" name="addScore1" id="addScore1" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $oldscore1; ?>" />
<input type="submit" />
</p>
<p><?php echo $player2; ?> current score: <?php echo $oldScore2; ?>
<label for="addScore2">Enter your score:</label>
<input type="text" name="addScore2" id="addScore2" />
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" />
<input type=\"submit\"/>
</form>
</p>
</div>
</body>
</html>

PHP echo within form value

I'm attempting to echo a div tag within a HTML form value. Im unable to display the contents of the div tag without messing up the form value.
<?php echo '<div id="demo"></div>';?>
Form value...
<input type="text" id="term" value="<?php echo '<div id="demo"></div>';?>">
What's outputted... (What's echoed inside the form value should be the current longitude and latitude)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Geolocation search</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script id="data" type="text/javascript" src="js/search.js" xmlData="data/data.xml"></script>
</head>
<body onload="getLocation()">
<div id="controller">
<label>Search Term:
<input type="text" id="term" value="<?php echo '<div id="demo"></div>';?>">
</label>
<label>
<select name="category" id="category">
<option value="name">Name</option>
</select>
</label>
<input name="Search" type="button" id="searchButton" value="Search">
</div>
<div id="result"> </div>
<?php echo '<div id="demo"></div>';?>
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML=" " + position.coords.latitude +
"<br> " + position.coords.longitude;
}
</script>
</body>
</html>
Is there soem other way to echo out the longitude and latitude from the that's loaded within the echo statement?
it's the "
change
<input type="text" id="term" value="<?php echo '<div id="demo"></div>';?>">
to
<input type="text" id="term" value="<?php echo "<div id='demo'></div>";?>">
You have double quotes around the word demo that are closing your value.
Try:
<?php echo "<div id='demo'></div>";?>
<label>Search Term:
<input type="text" id="term" value="<?php echo '<div id="demo"></div>';?>">
</label>
What i see from the above script is, you used a double quote to represent your attribute value.. and then single quote to handle the php echo value, then used a double quote again inside the div which makes the php code invalid because it stops its reading by the double quote you used. you might as well concatenate your php script or use an escape character for the string to be valid "\"
<label>Search Term:
<input type="text" id="term" value="<?php echo '<div id=\'demo\'></div>'; ?>" />
</label>
<?php echo "<div id='demo'></div>"; ?>
And may i just ask... What are you trying to echo out of the value? Is it really the div tag?
Its because the HTTP headers, which you are sending like above, are telling the browser to expect a file to be downloaded. They are not telling browser to expect HTML content (default). Therefore the content you are sending in an echo is not getting displayed.
Both won't work together correctly although I'm sure you can find any work around but the point is that you only need those echo's while debugging so you can disable the mentioned header call while debugging and once everything is set, enable that again so file can download.

I cant transfer html data with form above 3 pages

I have 3 pages.
a1.php : add record
a2.php : do sth
a3.php : record them database
But problem is occured, transfering "html data" from a2.php to a3.php.
EXAMPLE: a record which I try to record to a1.php is that :
<form name="form1" type="post" action="sayfa_1.php">
<input type='submit' value=' gidiyoruz' >
<input type='submit' value=' gidiyoruz' >
</form>
a1.php --> a2.php --> a3. php
On a2.php, there i no problem. But then. On a3.php when I show coming data, I SEE TWO BUTTON, NO HTML CODES which is above.
pages is below.
thanks.
a1.php
<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript">
//<![CDATA[
bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
//]]>
</script>
<form name="form1" type="post" action="a2.php">
<textarea rows="20" cols="90" id="anlam" name="anlam" style="overflow:auto;" > </textarea>
<input type="submit" />
</form>
a2.php
<?php
$anlam=$_GET["anlam"];
echo $anlam;
?>
<?php echo "<form name='fm' id='fm' action='a3.php' method='get'>"; ?>
<textarea rows="2" cols="50" name="anlam" style="visibility:hidden;" /> <?php echo $anlam; ?> </textarea>
<input type="submit" />
</form>
a3.php
<?php
$anlam=$_GET["anlam"];
echo nl2br($anlam);
Pace images are above. error: a3.php (instead code, there are two button)
so you want the page to send your data from one to another when you click submit button!!
Use if condition with isset.
if(isset($_POST['name_of_submit_button']))
{
// your code of next page
}
n similarly continue with another page by giving another name to you submit button and applying the same condition!!
Try using
method="post"
for your form. You could also just use an
<input type="hidden" name="anlam" value="<?php echo $anlam; ?>"/>
to transport your variable content to the last page. Are you sure, that the hidden textarea/input contains the correct value? Check the source code in your browser for this step.

Categories