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.
Related
I have some data in a session and I want to save it in text box value, using PHP. But the problem is when saving, just first token of string will be saved, like below example:
<?php session_start();?>
<html >
<head>
</head>
<body>
<form >
<?php echo $_SESSION['institute']="rebaz salih" ?>
<input type="text" <?php echo "value=".$_SESSION['institute']; ?> required />
</form>
</body>
Output will be:
rebaz salih
rebaz
Couple of things:
<?php echo $_SESSION['institute']="rebaz salih" ?>
Is this supposed to be the assignment? or just a print line? in any case, try getting rid of the echo.
Both #edCoder, and #chethan194 are on the right track... the value belongs on the outside, but you really should be using a new variable for institute. For example:
//htmlspecialchars will clear out any quotes, etc so it will work in the browser.
<?php
$value = htmlspecialchars($_SESSION['institute'], ENT_QUOTES);
?>
<form >
<input type="text" value = "<?php print $value; ?>" required />
</form>
Here is the full example:
<?php
php session_start();
//i don't know where you get the institute, so i will leave it here for now...
$_SESSION['institute']="rebaz salih";
$value = '';
if (!empty($_SESSION['institute']))
{
$value = htmlspecialchars($_SESSION['institute'], ENT_QUOTES);
}
?>
<html>
<head>
</head>
<body>
<form>
<input type="text" value = "<?php print $value; ?>" required />
</form>
</body>
You can put the value attribute outside php
<?php echo $_SESSION['institute']="rebaz salih" ?>
Wrong - <input type="text" <?php echo "value=".$_SESSION['institute']; ?> />
Right - <input type="text" value = "<?php echo $_SESSION['institute'];?>" />
</form>
<input type="text" value="<?php echo $_SESSION['institute']; ?>" required/>
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>
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
I have a form on my page that asks users to enter in their height/weight and it calculates their BMI and stores it in a database. I am hung up on how I transfer the javascript variable to a php variable. I understand I have to use hidden forms but I can't seem to figure out how they work.
here is my code
<?php include "base.php"; ?>
<?php session_start (); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>BMI</title>
</head>
<body>
<form id="bmi" action="main.php" method="post">
<p>Enter your height(in inches):</p><input type="text" id="height box" name="height box">
<p>Enter your weight(in pounds):</p><input type="text" id="weight box" name="weight box">
<input type="hidden" id="bmi" name="bmi">
<input type="button" value="Calculate" id="calc button" name="calc button" onClick="calc()">
</input>
</form>
<script type="text/javascript">
function calc()
{
// get variables from textboxes
var height=document.getElementById('height box').value;
var weight=document.getElementById('weight box').value;
var bmi=document.getElementById('bmi').value;
// calculate BMI
weight/=2.2;
height/=39.37;
BMI=Math.round(weight/(height*height));
</script>
<?php
//insert username, date, and bmi into the db
$username = mysql_real_escape_string($_SESSION['Username']);
$date = date('Y-m-d');
$bmi = mysql_real_escape_string($_POST['bmi']);
mysql_query("INSERT INTO bmi (username, bmi, date) VALUES('".$username."', '".$bmi."', '".$date."')");
?>
</body>
</html>
base.php is just where I do my connect to the sql server and select the database stuff
The javascript bmi variable does not seem to be transferring to the php $bmi variable. What is it I am doing wrong?
Set the value of the hidden input bmi to the calculated value.
<form id="bmi" action="main.php" method="post">
<p>Enter your height(in inches):</p><input type="text" id="height box" name="height box">
<p>Enter your weight(in pounds):</p><input type="text" id="weight box" name="weight box">
<input type="hidden" id="bmi" name="bmi">
<input type="submit" value="Calculate" id="calc button" name="calc button" onClick="calc()">
</form>
<script type="text/javascript">
function calc()
{
// get variables from textboxes
var height=document.getElementById('height box').value;
var weight=document.getElementById('weight box').value;
var bmi=document.getElementById('bmi').value;
// calculate BMI
weight/=2.2;
height/=39.37;
document.getElementById('bmi').value=Math.round(weight/(height*height));
}
</script>
Since you are not displaying the bmi to the user before he submits the form, make life easy for yourself. Use php code to calculate the bmi instead of javascript.
A few problems I see in your code:
In HTML you cannot have id and name attributes with spaces (http://www.w3.org/TR/html4/types.html#type-id)
You are calculating the bmi but aren't assigning it to the form element.
You are missing closing brace on the javascript calc() function.
The solution would be to fix the id attribute first, for e.g you could use camel case notation like follows:
<form id="bmi" action="main.php" method="post">
<p>Enter your height(in inches):</p><input type="text" id="heightBox" name="heightBox">
<p>Enter your weight(in pounds):</p><input type="text" id="weightBox" name="weightBox">
<input type="hidden" id="bmi" name="bmi">
<input type="submit" value="Calculate" id="calcButton" name="calcButton" onClick="calc()">
</form>
Update the javascript like follows:
function calc()
{
// get variables from textboxes
var height=document.getElementById('heightBox').value;
var weight=document.getElementById('weightBox').value;
var bmi=document.getElementById('bmi');
// calculate BMI
weight/=2.2;
height/=39.37;
bmi.value=Math.round(weight/(height*height));
}
Another point to note is that you are using mysql_ extensions which are deprecated. You want to start using mysqli or PDO instead.
I have simplified your code to 3 parts for better understanding, hopefully:
HTML: I'm using GET method instead of POST to see the variable on the URL. Please also not that input does not have close bracket </input>
<form action="index.php" method="GET">
<input type="text" name="height">
<input type="submit" onclick="calc();">
</form>
JavaScript: window.location.href tells the browser to show the variable on the URL
function calc(){
var height = document.getElementById('height').value;
window.location.href = "index.php?height=" + height;
}
</script>
PHP: Now you can retrieve the variable by using $_GET['height']
<?php
$height = $_GET['height'];
echo $height;
?>
I am probably making some silly mistake but I am newer to the world of Jquery and am looking for some help with this issue.
I have a form that I need to check/validate two dates once they change their values. Once they change I had an Ajax call that loaded a page "checkdates.php" and passed in two bits of data. The Date that changed and a project name to "checkdates.php" using GET and adding the data to the URL string. This Ajax call loaded the page in a div with the ID "status" and php page displayed what the outcome of the date check was.
The Old code used prototype to pull in the data as a function and I used the onChange event on the form elements:
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
/*function checkDate(date,project){
if(date!==''){
new Ajax.Updater('status', 'datechecker.php?date='+date+'&project='+project, { method: 'get' });
} else {
alert('enter a valid date!');
}
}
</script>
The code I'm trying to move over to jquery now looks like:
<script type="text/javascript">
$(document).ready(function() {
$('input#date_to').change(function() {
var datevalue = $(this).val();
$('#status').load('datechecker.php?date='+datevalue+'&project='+<? echo $_COOKIE['department']; ?>);
}
);
$('input#date_from').change(function() {
var datevalue = $(this).val();
$('#status').load('datechecker.php?date='+datevalue+'&project='+<? echo $_COOKIE['department']; ?>);
}
);
});
</script>
I think it may be an issue probably with how i'm pulling in my data or how i'm asking it to check the .change feature because it's not firing. Any assistance would be wonderful. Again the disclaimer: I'm sort of new to jquery so please be nice if it's some obvious idiot mistake.
EDIT:
As requested the HTML/PHP of the rest of the file (sorry for the formatting, this was a quick and dirty project that was last minute but turned into a huge headache):
<form id="form1" name="form1" method="get" action="?page=post_request">
<div class="timeavailable">
<?
require('data.php');
$name=$_COOKIE['un'];
$query="SELECT * FROM time WHERE emp_name='".$name."'";
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
$i=0;
?>
Vac: <b><? echo mysql_result($result,$i,"vacationtime"); ?></b>
Personal: <b><? echo mysql_result($result,$i,"personaltime"); ?></b>
Pts: <b><? echo mysql_result($result,$i,"points"); ?></b>
<input type="hidden" name="vacationtime" value="<? echo mysql_result($result,$i,"vacationtime"); ?>">
<input type="hidden" name="personaltime" value="<? echo mysql_result($result,$i,"personaltime"); ?>">
</div>
<h1>Time Off Request Form</h1>
<div class="header">
<div class="floatydetails">
<br>
Reason for Absence:<br>
<textarea name="reason_detail" id="reason_detail" rows="<?php echo $textareaheight; ?>" cols="<?php echo $textareawidth; ?>"></textarea>
<div id="status"></div>
<div id="daysrequested"></div>
</div>
<div class="col1">
Type of Absence Requested:<br>
<label for="reason_for_request"></label>
<select name="reason_for_request" id="reason_for_request">
<option value="Sick" >Sick</option>
<option value="Vacation" >Vacation</option>
<option value="Bereavement" >Bereavement**</option>
<option value="Doctor Appointment" >Doctor Appointment**</option>
<option value="Court" >Court*</option>
<option value="Jury Duty" >Jury Duty*</option>
<option value="Personal Day" >Personal Day</option>
<option value="Other" >Other</option>
</select><br>
<div class="col2">
Date of Request (mm/dd/yyyy)<br/>
<label for="date_from">From:</label>
<input type="text" name="date_from" id="date_from" class="required" /><br>
<label for="date_to">To:</label>
<input type="text" name="date_to" id="date_to" class="required" /><br>
Partial Request (HH:MM am/pm)<br/>
<label for="date_from">From:</label>
<input type="text" name="partial_from" id="partial_from" class="date-pick" /><br>
<label for="date_to">To:</label>
<input type="text" name="partial_to" id="partial_to" class="date-pick" /><br>
</div>
</div>
<div class="clear-fix"></div><br><br>
<hr>
<center><b>
Note: If You do Not have the Time Available, Your request will be Denied.<br>
* Proper Documentation is required Before approval is made. ** Proper documenation is required upon return.
</b></center>
</div>
<input type="hidden" name="do" value="post">
<input type="hidden" name="emp_name" value="<? echo $_COOKIE['un']; ?>">
<input type="hidden" id="projects" name="projects" value="<? echo $_COOKIE['department']; ?>">
<input type="hidden" name="supervisor" value="<? echo $cms->grabItemByName(employees, $_COOKIE['user_id'], supervisor); ?>">
<input type="hidden" name="emp_number" value="<? echo $cms->grabItemByName(employees, $_COOKIE['user_id'], employee_id); ?>">
<input type="hidden" name="page" value="post_request">
</form>
Is $_COOKIE['department'] an integer? Otherwise you'll probably have a syntax error there.
Both the functions seem to be identical so you can combine them like this:
<script type="text/javascript">
$(document).ready(function() {
$('input#date_to,input#date_from').change(function() {
var datevalue = $(this).val();
$('#status').load('datechecker.php?date='+datevalue+'&project='+<? echo json_encode($_COOKIE['department']); ?>);
});
});
</script>
Here is a possible answer:
jquery change event trigger
Also, try watching the console in Firebug to make sure your php page is getting/returning the correct values.
I found a solution after some of your help and some trial and error. Using the .ajax I was able to get more flexiability to do what I wanted.
<script type="text/javascript">
$(document).ready(function() {
$('#date_to,#date_from').change(function() {
//$('input').change(function() {
var datevalue = $(this).val();
var project = $('#projects').val();
$('#status p').load('datechecker.php?date='+datevalue+'&project='+project);
});
});
</script>
Hope this helps someone out.