Run script and php after form submission - javascript

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

Related

I want generate an alert dialog if login fail in PHP,JS but finding issue

if($row['psw']===$passwordphp)
{
echo '<script language="javascript">';
echo 'alert("LOGIN SUCCESSFULL")';
echo '</script>';
header("Location: registration.php");
}
else{
echo '<script language="javascript">';
echo 'alert("CHECK EMAIL OR PASSWORD")';
echo '</script>';
header("Location: login.php");
}
Here is the code. If I don't add header then JavaScript runs but when I add header Javascript stop running only executes the header function. Why is so? How can make both of them work properly?
A Location header tells the browser to drop everything and do something else. You can't do a Location redirect and output HTML.
That said, this code should throw a "Headers already sent" error message, as you can't do a header call after echoing anything to the browser.
As mentioned in the other answer, you can't send a header redirect and also send HTML.
Instead of the header redirect, you can use a Javascript redirect after the alert.
if($row['psw']===$passwordphp) {
echo '<script language="javascript">';
echo 'alert("LOGIN SUCCESSFULL");';
echo 'window.location = "registration.php";';
echo '</script>';
} else {
echo '<script language="javascript">';
echo 'alert("CHECK EMAIL OR PASSWORD");';
echo 'window.location = "login.php"';
echo '</script>';
}

save value when going from one html to another

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>

Stripe Simple Checkout Custom Data Field

I am trying to use Stripe's simple checkout system.
I want to include a custom field so that I can match an item_id to an order in my database.
https://stripe.com/docs/checkout#integration-simple
They don't seem to mention it in their documentation but it seems pretty essential for online services. How do I attach a custom id field that will be recorded with the order?
echo '<form action="/charge" method="POST">';
echo '<script ';
echo 'src="https://checkout.stripe.com/checkout.js" class="stripe-button" ';
echo 'data-key="pk_test_ksdjhg8dsgsghdsgh" ';
echo 'data-email="email#example.com" ';
echo 'data-name="example.com" ';
//echo 'data-bitcoin="true" ';
echo 'data-description="Campaign" ';
echo 'data-currency="usd" ';
echo 'data-amount="2000" ';
echo 'data-locale="auto">';
echo '</script>';
echo '</form>';
I hope you require to send your custom data-field with stripe checkout and trying to match with your data base. If I'm right? Follow below detail.
For include custom data you need to use hidden input like below:
echo '<form action="/charge" method="POST">';
echo '<script ';
echo 'src="https://checkout.stripe.com/checkout.js" class="stripe-button" ';
echo 'data-key="pk_test_ksdjhg8dsgsghdsgh" ';
echo 'data-email="email#example.com" ';
echo 'data-name="example.com" ';
//echo 'data-bitcoin="true" ';
echo 'data-description="Campaign" ';
echo 'data-currency="usd" ';
echo 'data-amount="2000" ';
echo 'data-locale="auto">';
echo '</script>';
echo '<input name="item_id" value="SOMEVALUEHERE" type="hidden">';
echo '</form>';
In above code, I include hidden for item_id. You can get the value from that input field in your PHP page like:
$item_id = $_POST["item_id"];
My experience, which is that custom fields are not possible using the basic Stripe checkout, concurs with the accepted answer on this very similar question:
How to add custom fields to popup form of Stripe Checkout
I believe the best route forward would be to get handy with stripe.js and create a custom payment form:
https://stripe.com/docs/custom-form
I am working around that at present by using different endpoint URLs in the form actions to do different things. This works fine, but I may end up using stripe.js in the production code.
you could do something like this to add extra information
echo '<form action="/charge?item_id=123" method="POST">';
and use $_REQUEST['item_id'] in stead of $_POST['item_id'] in the charge page to get the value.
Not my preferred way of working, but in this case it solves the problem.

Adding action to a next step in a js modal window

I have a normal css/html/js modal window and it is a simple contact form. It has a send button that I'd like to turn into a Thank You modal window after the contact form is processed via php. How would I go about doing this?
Try to put this on your code
<?php
if($send)
{
echo"<script type='text/javascript'>";
echo "alert('Thank You.');";
echo "</script>";
}
else{
echo"<script type='text/javascript'>";
echo "alert('Fail.');";
echo "</script>";
}
?>

sending form data to email with php

I wrote a php page that send the form data to my email.
The problem is that when the user click on submit, he automatically moved to a blank php page. I want that instead of moving to a blank php page, the user stay on the same page and instead of the form fields, he see a message like "thank you for contacting us". How can I do this? and how can I style this message?
In addition, all the form data appears on the email subject (not in the body of the message). there is any way to change it?
Here is the PHP:
<?php
error_reporting(E_ALL);
ini_set("display_errors", "On");
$subject="Message from Web";
$sender=$_POST["user-name"];
$senderEmail=$_POST["email"];
$senderPhone=$_POST["phone"];
$senderCompany=$_POST["company"];
$message=$_POST["message"];
$mailBody="Name: $sender\nEmail: $senderEmail\nPhone: $senderPhone\nCompany: $senderCompany\n\n$message";
mail('mymail#gmail.com', $mailBody, $sender);
$thankYou="<p>Thank you! Your message has been sent.</p>";
?>
<form method="post">
<input type="text" name="to-e-mail">
<input type="text" name="subject">
<textarea name="message">
</textarea>
<input type="submit" value="send">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$headers = 'From: sender#mail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($_POST['to-e-mail'],$_POST['subject'],$_POST['message'],$headers);
print 'thank you for contacting us';
}
?>
Very simple example how to send mail with php.
Please check the security requirements.

Categories

Resources