i previously asked this question but the example i provided did not provide my real problem.
I want to get the input from a form using a php variable and use it in a javascript function. I do not use javascript directly because the content of the variable in question is obtained from a mysql database and i do not think that there is a way for me to access the database using javascript.
below its the code that i have so far:
<!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var slon1 = '<?php echo (json_encode($siteLon1)); ?>';
self.alert(slon1);
//some code goes here.
}
</script>
</head>
<body>
<div align="center">
<form method="get">
<label>Choose Site</label>
<select name='ps' id="ps" data-inline="true">
<?php
$use = $fgmembersite->getsites($fgmembersite->UserFullName());
$fields_num = mysql_num_fields($use);
while($row = mysql_fetch_row($use))
{
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
?>
</select>
<input type="submit" name="submit" value="View">
</form>
</div>
<div id="map-canvas"></div>
</body>
</html>
Suprisingly enough, the result displayed on self.alert(slon1) is null; be it when i just load the page or when choose from the drop down box and click on View. Please someone help me on this.
Thanks.
JSON encoder should quote variable, so using single quotes manually would trigger JavaScript parse error. Check JS console. This should generate valid JS code:
var slon1 = <?php echo (json_encode($siteLon1)); ?>;
Related
I created a javascript file, made a function, and then linked my html file with it in the head. It worked. When I copy and pasted the exact same thing into a php file, the function suddenly doesn't work. I need a way to make the function work in the php file.
This is the javascript function:
function showField(){
if(document.getElementById("downtown").checked && document.getElementById("pricerange").value == "100"){
document.getElementById("newField").removeAttribute("hidden");
}
}
This is the php where i try to call the function:
<input type="button" name = "search" value="Search" id="search" onclick="showField();">
This is in my head where I linked the javascript file:
<script src = "a3q5.js"></script>
Seeing that I don't know what the rest of your code is, I would just make sure you have the normal HTML stuff:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="button" name = "search" value="Search" id="search" onclick="showField();">
<script src = "a3q5.js"></script>
</body>
</html>
Hope this helps!
The simple way would be here, Put this to your PHP. It worked for me
<?php
// your code
echo '<script src = "a3q5.js"></script>';
// your code
?>
I am working on my website which having different tabs (home, contact, etc.). Now in one of these tabs I'm using a login form, in this form I'm just validating one fixed user (Not Using database), this will understand from verification.php code.
Below is part of .html file code that calling verification.php file
<div id="loginn" class="loginForm">
<form action="verfication.php" method="post">
<div class="ll">
<img src="image/avatar2.png">
</div>
<label for="name"><b>Username</b></label><br>
<input type="text" name="uname" required><br>
<label for="psw"><b>Password</b></label><br>
<input type="password" name="psw" required><br>
<button type="submit">Login</button>
</form>
</div>
Below is my verification.php file, which is saved by me in www directory, where all the html, css, js files are stored
<!DOCTTYPE html>
<html>
<body>
<?php
if(isset($_POST['uname']) and isset($_POST['psw']))
{
$name=$_POST['uname'];
$pass=$_POST['psw'];
if ( $name == "rrrr" and $pass="ravina#6543" )
{
?>
<script type="text/javascript">document.getElementById('veri').style.display="block";</script>
<script type="text/javascript">document.getElementById('loginn').style.display="none";</script>
<?php}
}
?>
</body>
</html>
Now the problem is, when I click on the login button, there is nothing happening on the html part. I mean, my PHP file is not getting access from my HTML part, so please can anyone help me to solve these problem?
I think there's an error in the syntax: first of all there is an additional "T" in the <!DOCTTYPE html> and secondly there's a curly bracket after you started the php script: <?php}, just adding a space will fix your problem.
There's also a spelling mistake in your html code regarding the name of the php file verification.php.
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['uname']) and isset($_POST['psw']))
{
$name=$_POST['uname'];
$pass=$_POST['psw'];
if ( $name == "rrrr" and $pass="ravina#6543" )
{
?>
<script type="text/javascript">document.getElementById('veri').style.display="block";</script>
<script type="text/javascript">document.getElementById('loginn').style.display="none";</script>
<?php }
}
?>
</body>
</html>
in your verification.php, you can check success/not with using only echo
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['uname']) and isset($_POST['psw']))
{
$name=$_POST['uname'];
$pass=$_POST['psw'];
if ( $name == "rrrr" and $pass="ravina#6543" )
{
echo "success~~";
} else {
echo "failed!!";
}
}
?>
</body>
</html>
This is not how to use PHP, HTML and javascript. You should start by learning how HTTP works, then HTML, then JavaScript and finally PHP.
First of all you should correctly write the name of php file in your html form i.e. verification.php
verification.php file remove one T from DOCTTYPE html and give one space between php and { in ?php{ this line.
This might go very basic, but I am not able to understand what is the best way to call AJAX on a button click event on page and get the data from the server to be displayed using php.
What I have is a simple webpage called div.php:
<html>
<head>
<title>
Test
</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js">
$(document).ready(function(){
$('#btn').click(function(){
$("#data").html('Loading...');
$.ajax({
url:'test.php',
type:'GET',
success:function(data){
$("#data").html(data);
}
});
});
});
</script>
</head>
<body>
<form method="get">
<button id="btn">
Get Data from PHP file
</button>
<div id="data">
</div>
</form>
</body>
</html>
And then a page behind it doing the database operation, test.php:
<?php
include ("config.php");
$sql = "SELECT * FROM userInfo;";
$result = mysql_query($sql);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$count = mysql_num_rows($result);
if ($count > 0) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $row["userLogin"] . "<br>";
}
}
?>
It is pretty basic and I am supposed to get the query result on the button click, but it doesn't work. Is there something wrong in here?
Any help or ideas to understand PHP to AJAX to JS flow will be really appreciated.
You need to embed the JS separately, you can't do what you've done but need to split as below.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#btn').click(function(){
... etc
});
});
</script>
This is difficult to debug based on the limited info available, although I think this may be the issue. Your <button> element is inside a <form> element. This means that when you click the button, it is submitting the form and reloading the page. Your AJAX may have worked but the page has reloaded so you won't see the data. Solution:
Either remove your <form> from the page or look into e.preventDefault() for the button click function in jquery.
On another note, you should migrate your code to using another library such as PDO for accessing databases as the mysql_* functions should no longer be used.
I am newbie learning PHP, and I think my problem is that I can't pass a variable value from HTML to PHP:
I have one page, called form.php. Here it is its code, along with its HTML code as well:
<html>
<head>
<title>form</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form action="page2.php" method=post>
My name is: <br>
<input type="text" name="yourname">
<p> Please leave your message here <br>
<input type="text" name="message">
<p>
<input type="submit" name="submit" value="Please accept my data!">
</form>
</body>
page2.php has this chunk instead in it:
<html>
<head>
<title>Hi!</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>
Hi! <?php print $yourname; ?>
<p>
Thank you for your message <b> <?php print $message; ?> !?! </b>
</body>
The error I get is the following:
Notice: Undefined variable: yourname in C:\xampp\htdocs\Test-Antonio\page2.php on line 7
Notice: Undefined variable: message in C:\xampp\htdocs\Test-Antonio\page2.php on line 9
So it seems that I am not able to pass varibles values from HTML to PHP. Is it right? What I am supposed to do to make it run?
Thank you in advance to anyone who can help me on this!!
Best regards,
Antonio.
Page 1 should be:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page 1</title>
</head>
<body>
<form action="page2.php" method="post">
<label>My name is: </label><br/>
<input type="text" name="yourname"> <br/>
<label> Please leave your message here:</label><br/>
<input type="text" name="message"><br/>
<input type="submit" name="submit" value="Please accept my data!">
</form>
</body>
</html>
Page 2 should be :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page 2</title>
</head>
<body>
<p>Hi! <?php echo $_POST['yourname']; ?><p>
<p>Thank you for your message <b> <?php echo $_POST['message']; ?> !?! </p>
</body>
</html>
Your syntax is wrong. You should echo $yourname with
<?php echo $yourname ?>
OR
Also make sure you've assigned the $yourname variable. You can do that with
$yourname = "your name";
Or if you are working with a post mechanism:
$yourname = $_POST['yourname'];
<?php echo $_POST ["yourname']; ?>
Will resolve your error. Because you have not assigned a variable for $yourname PHP doesn't know what it's doing so will throw an error. I'd also recommend looking at the manual for the $_POST global
http://php.net/manual/en/reserved.variables.post.php
You can get the variables from a form using $_POST['variablesName']. So in this case , use $_POST['yourname'] other than $yourname.
use $_POST to get the data passed via POST request
Hi! <?php echo $_POST["yourname"]; ?>
Your html form has method="post" attribute so it will pass all your input via POST request and can be accessible in PHP in $_POST[<input name>] variable.
If your form has no method attribute defined or has method="get" then it will be submitted via GET request and can be accessible in PHP via $_GET[<input name>].
Read this: http://www.w3schools.com/php/php_forms.asp
Learn about basic form handling in PHP and HTML please. You might want to learn ajax via javascript to tweak your forms. Have a read about ajax here: https://developer.mozilla.org/en/docs/AJAX
I got no output when I use variable in val() function instead of fixed value. How can I fix this error.
Example: In the below example, when I use variable ($data="Glenn Quagmire"), I get no output.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val($data);
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>
Regards
Just use like this:
$("input:text").val("<?php echo $data; ?>");
Full code
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?php echo $data; ?>");
});
});
</script>
Don't use "$" for variables (that's PHP) in jQuery.
<?php echo 'data="Glenn Quagmire";'; ?>
$("input:text").val(data);
JavaScript running in the browser cannot access variables from a PHP program (which has finished running on another computer before delivering the page to the browser so the variable does't even exist any more).
You have to make PHP output the data to the JavaScript.
You can encode it using json_encode (because JSON is (more or less) a subset of JavaScript literal syntax). This will add quotes as needed and escape any special characters in your data (you don't have any in your example, but this is a safe, general approach that protects you from XSS).
$("input:text").val(<?php echo json_encode($data); ?>);
Use <?= $variable ?> to embed variable values in HTML, e.g.:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?= $data ?>");
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>