I am trying to send a PHP variable to my javascript. In order to do this, I encode the php variable with json, send it to my view with my controller, use it as a value in a hidden html form, and then pull the value with js, using parseJSON to get the final product.
HTML (I'm using laravel, the brackets are equivalent to php tags):
<input type="hidden" id="js-helper-artist-likes" name="js-helper-artist-likes" value="{{$artist_likes}}">
JS
var artist_likes = $('#js-helper-artist-likes').val();
console.log(artist_likes);
In the page source, I see that the value is populating with the json string, but when I print it to the console from my js like the above, all that is showing is: [{
I am not sure why this is.
I want to perform:
var artist_likes_decoded = $.parseJSON(artist_likes);
to get the decoded variable in JS, but this is impossible without the encoded string coming up correctly in the artist_likes variable in js.
Any ideas what I'm doing wrong? Thank you.
<input type="hidden" id="js-helper-artist-likes" name="js-helper-artist-likes" value="{{ echo $artist_likes}}">
You could use:
<input type="hidden" id="js-helper-artist-likes" name="js-helper-artist-likes" value="<?= json_encode($artist_likes) ?>">
<?php
$yourVariable ="something";
$arr = json_encode(array("name"=>$yourVariable));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<input type="hidden" id="js-helper-artist-likes" name="js-helper-artist-likes" value='<?php echo $arr; ?>'>
<script>
$(document).ready(function(){
var artist_likes = $('#js-helper-artist-likes').val();
console.log($.parseJSON(artist_likes));
});
</script>
</body>
</html>
Related
Here's javascript code that i have
var randomnum = 30;
and here's PHP code
<?php $_SESSION['numbertoguess'] = '<script>document.write(randomnum)</script>';?>
But this is not passing the value
But when i am trying this code below, it works. It gives session variable that the value 'a sample thing'.
<?php $_SESSION['numbertoguess'] = 'a sample thing';?>
Look at the following code. The PHP session is assigned to 30 from the Javascript value. however am not sure if this is good way for implementation.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Html Page Title</title>
</head>
<body>
<script>
var randomnum = 30;
</script>
<?php $_SESSION['numbertoguess'] = '<script>document.write(randomnum)</script>';?>
<?php echo $_SESSION['numbertoguess']; ?>
</body>
</html>
You can't access Session directly in JavaScript.
You can make a hidden field and pass it to your page and then use JavaScript to retrieve the object via document.getElementById
This question already has answers here:
How to call a JavaScript function from PHP?
(12 answers)
Closed 6 years ago.
I have a js file.
jiten.js
function myFunction(p1, p2) {
return p1 * p2;
};
I have to call this function from php file
Demo.php
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Mosquitto Websockets</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="jiten.js" type="text/javascript"></script>
</head>
<body>
<?php
echo '<script type="text/javascript">myFunction(2,4);</script>';
?>
</body>
</html>
But When I run this file, I get nothing....
PHP and JavaScript are different languages, they can be embedded but, each language has to call its own functions.
You are returning value through JavaScript code, but, you can not echo it with PHP and not printing it through JavaScript.
You need to print the value by using document.write() in JavaScript.
Corrected Code:
<script type="text/javascript">
function myFunction(p1, p2) {
return p1 * p2;
};
</script>
<?php
echo '<script type="text/javascript">document.write(myFunction(2,4));</script>';
?>
Output:
8
Your code is working well, but your Javascript function doesn't display anything, it just returns a value.
You should use something like console.log() or alert() to display the result.
Try This
echo '<script type="text/javascript">document.write(myFunction(2,4));
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
not see data on the textarea
creatDocument.php
<html>
<head>
<script type="text/javascript" src="../jquery-1.8.1.js"></script>
<script language="javascript">
function insertFormulationToTextErea(string){
document.getElementById("argumentId").value = string;
}
</script>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<textarea name="argument" id="argumentId" rows="10" cols="50"></textarea>
<?php
if(isset($_REQUEST['formulation']))
$formulation = $_REQUEST['formulation'];
else $formulation = "No data receive";
$formulation = base64_decode($formulation);
echo "<script>insertFormulationToTextErea('$formulation')
</script>";
echo $formulation;
?>
</body>
</html>
$formulation is the string parameter that received from another php page with POST method.
When I use with echo I see the string value, when I try to insert $formulation to textarea, the textarea is empty.
When I use with GET method it work fine, but when $formulation string too long, the server reported that Request-URI Too Large.
Anybody know this problem or any solution that I can use?
What you're doing looks a bit complex, try simply this:
<textarea name="argument" id="argumentId" rows="10" cols="50" value="<?php echo $formulation; ?>"></textarea>
This puts your string directly into the textarea value, without trying to use it as a parameter for your function.
AND reason why your you get nothing from $formulation when you use it as a paramter for your function is because your code should be like so:
echo "<script>insertFormulationToTextErea(".$formulation.")</script>";
Though I highly advise you to avoid coding like that..
I've a problem with iframe. My first file filtre.html contain iframe with a basic code like this:
<iframe name="filtre_demo" src="http://localhost:8888/modules/filtredemo/filtre.php" scrolling="no" height="220" width="220" frameborder="no"></iframe>
This file filtre.php is a drop down dynamic list with 3 levels (PHP/MYSQL).
Filtre.php this file is actually a dynamic dropdown 3 levels and will find info in a DB. The submit sent to a URL and all working fine.
My problem is with iframe. When I click on submit, target page is found in this part of 220x220 instead of reloading the parent page.
I tried several solutions to no avail. Here is part of my php file (I deliberately truncated several parts) including the header location and the form:
$ligne=mysql_fetch_assoc($rech_lien);
$lien=$ligne['lien'];
header('Location:'.$lien.'');
<!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" xml:lang="fr">
<head>
</head>
<body style="font-size: 75%; width: 210px;">
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post" id="chgcategories">
<fieldset style="border: 0px">
<select name="modele" id="modele" onchange="submit();">
<option value="-1">-Choisissez un modele-</option>
<select name="sous_categorie" id="sous_categorie">
<option value="-1">-Sous-cat-</option>
</select>
<input type="hidden" name="nb_listes" value="3" />
<br /><br />
<input type="submit" name="ok" id="ok2" value="Envoyer"/>
</form>
I also tried with javascript but without success. In this configuration there I removed the line:
header('Location:'.$lien.'');
Here is the javascript code in the header:
<!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" xml:lang="fr">
<head>
<script type="text/javascript">
parent.document.location.href="<?php echo $lien; ?>";
</script>
</head>
<body style="font-size: 75%; width: 210px;">
The problems in this case is that the document load loop.
Anyway, I'm stuck, I thank you in advance for your help.
Vincent
If I understand what's happening, the buttons are targeting the contents of the iframe and loading the page inside that, instead of the actual window? If that's the case, set the target attribute the either _parent or _top and that should target the actual window.
You can try this javascript code:
<?php
/*-------Your Php Code-----*/
\\Replace this code #header(location)
echo "<script>";
echo "window.location='yourloation.php';";
echo "</script>";
/*-------Your Php Code-----*/
?>