I have a scenario where I need an ID (from a PHP variable) in JavaScript. Which would be the best way of adding the value to the page?
<input type="hidden" name="variable" value="<?php echo $value; ?>">
or
var $variable = <?php echo $value; ?>;
Thanks for any feedback or advice.
Related
I have a button on my website that triggers an onclick event with the following function
onclick="
updatePage('<?php echo $page['title']; ?>','<?php echo $page['id']; ?>','<?php echo $page['attachedfiles']; ?>','<?php echo $page['attachmentprefix']; ?>');
"
this usually works fine but if the $page['title'] contains a single quote it breaks the rest for example if the string was "What's your name?" it would break after the "what '"
Is there a fix for this, i have tried using
echo htmlspecialchars($page['title'])
but it doesnt work.
I am still a beginner so i am not too sure on how to solve this.
I am sorry if this is a duplicate question but from other solutions ive seen this seems like more of a javascript issue rather than php
You can try the addslashes function. Example:
<div class="thediv"></div>
<?php
$title = "This isn't the title";
?>
<script>
var div = document.querySelector('.thediv');
div.innerHTML = '<?= addslashes($title) ?>';
</script>
I have an html form like this:
<form method="get" action="save.php">
<input type="email" id="email" name="email"/>
<input type="submit" name="submit" value="Submit" />
</form>
and in save.php i have something like this:
<?php
session_start();
$email = $_REQUEST['email'];
$email_content = "Thank you for your subscription";
mail($email,"Thank you",$email_content);
header("Location:thankyou.php");
?>
Now in save.php file i need to send this e-mail but also to echo a script that runs a js function. For example
<?php
echo "<script>";
echo "<script src='my_path_to_file/file.js'></script>";
echo "var subscriberEmail = '" . $email . "';";
echo "mySubscribe(subscriberEmail);";
echo "</script>";
?>
Now, if i place the echoing of the script before mail(), then i don't go to thankyou.php, mail() is not executed, i don't go to thankyou.php but script function works. If i place echoing of script after mail, then mail is sent, i go to thankyou.php but script function is not executed at all.
Any ideas to make both happen?
Thank you in advance
It's becouse echo command send content to browser, and header redirect will never works.
You could try to use comething like that:
<?php
echo "<script>";
echo "<script src='my_path_to_file/file.js'></script>";
echo "var subscriberEmail = '" . $email . "';";
echo "mySubscribe(subscriberEmail);";
echo "document.location.href='thankyou.php';";
echo "</script>";
?>
It means, move redirect command from php code to javascript.
Possibly some error in mySubscribe(subscriberEmail); function. Thats is why if you put script before email and header("Location:thankyou.php"); it not sending mail and redirects you. And also if you put script before, $email variable is not set yet
I have a value on a textBox on html:
<input type="text" id="Key" name="Key" required />
then I send action to php and I do this from php:
<?php
header("location:./setupDatos.html");//echo "2"; //first time activation
echo '<input type="text" value="' . $varKey. '" />';
The problem is, if I remove this line
header("location:./setupDatos.html");//echo "2"; //first time activation
it shows me a blank page with the key I sent to php (as expected)
but with the line, it changes to new html and the input with the key sent is nowhere. How can I send that value from first html to the next html?
The HTTP protocol is stateless, meaning it can't "remember" older post data. However you can use the session to simulate a state.
Try doing something like:
<?php
session_start();
$_SESSION["oldpostdata"] = $_POST;
header("location:./setupDatos.html");
Not sure what the point is here. An html page wouldn't normally be able to do any server-side processing. If it were able to however you'd access your old post data like:
setupDatos."html"
<?php
session_start();
$oldPostData = $_SESSION["oldPostData"];
unset($_SESSION["oldPostData"]); // To only "flash" the data and not have it persist
$id = $oldPostData["Key"]; // Probably
Alternative way to "forward" the post data taken from this question:
<!DOCTYPE html>
<html>
<body onload="document.forms[0].submit()">
<form action="new-location.php" method="post">
<?php foreach( $_POST as $key => $val ): ?>
<input type="hidden" name="<?= htmlspecialchars($key, ENT_COMPAT, 'UTF-8') ?>" value="<?= htmlspecialchars($val, ENT_COMPAT, 'UTF-8') ?>">
<?php endforeach; ?>
</form>
</body>
</html>
When I ask for the value of this hidden input:
<input type="hidden" name="theOrigin" value="<?=$_SESSION['origin'];?>">
all I get is
<?=$_SESSION['origin'];?>
Something wrong?
Thats because you are using incorrect Php tags.
Try this:
<?php ... ?>
So in your case the value of your element would look like:
value="<?php print($_SESSION['origin']); ?>"
I am simply trying to store $username in a file that will be included in all the pages after a user logins.Thus, this $username will change dynamically according to the user that has logged in.
includeusername.php
<?php
$username=$_POST['username'];
echo $username;
?>
The variable does get initialized in includeusername.php ,as echo gives the value.On including this file in the main page which will display "Welcome $username!" i write this code
Welcome <?php echo $username; ?>!
The error shown is Undefined index: username.
How to have access to data from other page?And transfer it to the next page too!
I've used the following but it didn't work:
sessionStorage()
$_SESSION['username']
include/require
Any other idea to have persistent data across the web pages?
<?php
session_start();
$_SESSION["username"]=$_REQUEST['username'];
?>
In your includeusername.php use
Welcome <?php echo $_SESSION["username"]; ?>!
start the see SESSION START
<?php
session_start();
$username=$_POST['username'];
echo $username;
echo "Assigning session value to: " . $username;
$_SESSION['username'] = $username;
?>