Stripe Simple Checkout Custom Data Field - javascript

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.

Related

How to echo php string with special characters inside javascript function parameter

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>

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

Filter WordPress posts by custom taxonomy term with AJAX

Please advise....
I create filter taxonomy using this tutorial: https://www.bobz.co/blog/demo-filter-wordpress-posts-custom-taxonomy-term-ajax/
It is echoing all my codes but when I click to certain tag it stop showing me images only.
I am using ACF Gallery code for images in template.
<?php
$images = get_field('gallery');
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<p><?php echo $image['caption']; ?></p>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I check ajax filter post file and try to edit code but still fails to do so can you please help me out with it.
I want gallery to be display with certain posts when I click on "TAGS"
$output = '<h2>'. get_the_title() . '</h2>';
$output .= get_the_excerpt();
$result = 'success';
endwhile; else:
$output = '<h2>No posts found</h2>';
$result = 'fail';
endif;
There is not a way we can say whats happening unless seeing it.
Additionally, if you want get the ACF fields from the current page then you must pass the post id. So, it might help you by guessing but not debugging your problem.
$images = get_field('gallery', $post_id);
$post_id should be the id of that particular post you want to show.
Hope this works as guessing your problem for now.
If you give us more details we can see and inspect the problem more easily.
Thanks

How to refresh the page in php after displaying alert

I am updating the records using php/mysql. and after all is done .
I run this code for user confirmation about the activity.
echo '<script language="javascript">';
echo 'alert("We Have credited your account")';
echo '</script>';
How Do i refresh the page so that once user clicks ok, it will refresh all the details again and should display users with new values from the db.
If I use this right after the alert.
header('Location: '.$_SERVER['REQUEST_URI']);
It does not displays the alert message and simply refreshes the page.
Alerts are weird but you can do something like the below as your echo. Alert returns an undefined.
echo "<script language='javascript'>";
echo "if(!alert('We Have credited your account')){
window.location.reload();
}";
echo "</script>";
UPDATE
Right after posting this I realized that the alert will block any other js from running. You can literally just do
echo "<script language='javascript'>";
echo 'alert("We Have credited your account");';
echo 'window.location.reload();';
echo "</script>";
Edit to handle page refresh (submit by OP):
echo "<script language='javascript'>";
echo 'alert("We Have credited your account");';
echo 'window.location.reload();';
echo "</script>";
} else {
echo '<script language="javascript">';
echo 'alert("Please Paste Exact URL Here")';
echo '</script>';
}
window.location.reload and header('Location: '.$_SERVER['REQUEST_URI']) will keep you in a loop "basically" not static "locations", if you die in game you get reloaded to a point or location, you can't replace where you spawn until you got past that point (^.^).
Use window.location.replace("your_page.php") or window.location.href = "your_page.php"; because reload and header is more used for auto redirection to different locations while replace and href is more used to "escape" your current page and load it from scratch.
So do something like this if your query passes:
if ($conn->query($sql) === TRUE) {
echo "<script language='javascript'>";
echo 'alert("Your alert msg");';
echo 'window.location.replace("your_page.php");';
echo "</script>";
}
else{
echo "FAIL";
}
It will prevent data insertion or strings from going in a loop over and over again. I had a pc's USB ports that malfunctioned once and it kept on inserting and alerting in on of my databases it was an EPIC disaster took a while cleaning it up.

display a div/span only after $_SERVER['HTTP_REFERER'] has been executed

I tried to approach it numerous ways, but somehow I'm not able to figure it out. Maybe you guys can help me?
I need to display a certain div/span or whatever container to display a text after page goes back via
header('Location: ' . $_SERVER['HTTP_REFERER']);
The reason why I need such a wierd approach to display something is that I'm using a href/GET combination to run a PHP function when my link gets klicked. (submit button is in use by another function/module so I can't use that )
HTML part
<p id="show_project">
<a id="add_to_project" name="add_to_project" href="?function">
Add to project
</a>
</p>
PHP function part
$this->getProductinfo();
if (isset($_GET['function'])){
$this->getSQLQuery($_GET['function']);
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
Any ideas ?
BR's
Why not add a message to your header() call
$this->getProductinfo();
if (isset($_GET['function'])){
$this->getSQLQuery($_GET['function']);
header('Location: ' . $_SERVER['HTTP_REFERER'].'?msg=We got a message');
}
Get the message
<?php
session_start();
if (!empty($_GET['msg'])){
$_SESSION['msg'] = $_GET['msg'];
header('Location: ' . $_SERVER['HTTP_REFERER']);
die;
}
else{
if (!empty($_SESSION['msg'])){
$msg = $_SESSION['msg'];
unset($_SESSION['msg']);
}
}
?>
<?php
/* Later in the document */
echo '<div>' . $msg . '</div>';
?>
You will need to sanitise and validate the $_GET variable
borrowed from here

Categories

Resources