save value when going from one html to another - javascript

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>

Related

Run script and php after form submission

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

How to run JS function on button click, using PHP form data?

I have got a form on my WordPress homepage that takes one input.
<form method='post' action='<?php bloginfo('url'); ?>/test123/' >
Name: <input type="text" name="name" required="required">
<input type="submit">
</form>
When submitted it redirects and passes the input to the test123 page (the page has a custom php template). If I add Hello <?php echo $_POST["name"]; ?> then it works without issue.
However, I have a JavaScript function that should run after the submit button is clicked. The users input name needs to be used inside the function.
My first thought was to use onclick="test();" but I don't believe that will work for calling the function on a different page, and I still have the issue of passing the PHP data into the JS function.
I've tried using <script type="text/javascript> .... </script> in test123 page's php template file with no luck.
Edit:
<?php /* Template Name: test123 */ ?>
<?php get_header(); ?>
Your email is <?php echo $_POST["email"]; ?>
<script type="text/javascript>
function testing() {
console.log(<?php echo $_POST["email"]; ?>)
}
testing();
</script>
<?php get_footer(); ?>
If you using jQuery in Wordpress
$('form').submit(function(e){
var name = $(this).find('input[name=name]').val();
// do smth with name
return true;
});

HTML hyperlink with Javascript onclick event

how to make a hyperlink to download the upload file and by the same time it updates its database?
For now I can download the file but it doesn't change the file status.
Here is my code
Docx
<script type="text/javascript">
Cufon.now();
function runMyFunction() {
<?php
mysql_query("update report_upload set status=1 from report_upload where year='$year' AND area='$area'",$conn);
?>
return true;
}
</script>
try1
.html
<form id='login' action='adminReportDownload.php' method='POST' enctype='multipart/form-data'>
<input type="hidden" value="<?php echo $area;?>" name="yeara">
<input type="hidden" value="<?php echo $year; ?>" name="yeary">
<input type="hidden" value="<?php echo $abc123; ?>" name="abc123">
<input type="submit" name="submit" id="submit" value="Download">
</form>
.php
$a = $_POST['yeara'];
$b = $_POST['yeary'];
$c = $_POST['abc123'];
$file_path = 'reports/'.$c;
$sqlABC = "UPDATE report_upload SET status='1', usedBy='$sesId' where year='$b' AND area='$a' ";
$eviABC = mysql_query( $sqlABC, $conn );
header("location: reports/$c");
Because PHP is server-side, it is executed before your browser displays your web page. Thus, your JavaScript function runMyFunction contains nothing but a return true; statement.
Especially since trying to do server-side operations from the client-side is a bad idea, a better solution may be to have the link point to a PHP page that sends the file and updates the database at the same time.
For further reading, this question about PHP readfile may be helpful.
you can achieve this by send post request to server when click on the link.
on the eventhandler, prevent the default behavior by e.preventDefault(); e.stopPropagation()
write an ajax method to post the request to sever
on serverside, update the database, write the file to frontend.

Safest way to get $_SESSION variable in Javascript/jQuery?

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.

taking Input field value in session variable and showing at action page

I have following code at s.php:
<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['p'] = $_POST['p'];
}
?>
<form action="s2.php" method"post">
<input type="text" name="p"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
And At s2.php
<?php
session_start();
?>
<?php
echo 'This is especially for ='.$_SESSION['p'];
?>
After entering value in input field and clicking the submit button, it take to next page and change the browser link to some thing like /s2.php?p=inputvalue&Submit=Submit.
I want to show the value at s2.php that was entered in the input field at s.php.
I have placed the echo code, but nothing shows up (I have tested on different servers).
The problem is solved. Thank you.
Solution: at s2.php (action page) we have to use the following code:
echo 'This is especially for ='.$_POST['p'];
Thanks

Categories

Resources