How to send the payment_method_nonce to the server? - yii2

My server is built with the yii2 Advanced framework.
I have integrated the Braintree Drop-In form.
The documentation says an invisible payment_method_nonce is created. My question is how should I send this to the server?
On clicking the default submit button in the Drop-In form (filled with one of the test card details) it accepts the info (UI changes with new Change payment method button) but the Post call never happens/doesn't reach the server.
How do I send the payment_method_nonce to the server?
Am I missing something here?
Thanks in advance.

I finally got it to work.
This is the yii2 view code I used:
<?php $form = ActiveForm::begin(); ?>
<div id="payment-form"></div>
<?= Html::submitButton('Submit',['class'=>'btn btn-success']); ?>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
var clientToken ='<?php echo $var ;?>';
braintree.setup(clientToken, "dropin", {
container: "payment-form"
});
</script>
Basic stuff with just the drop-in div

Related

yii2 how to use response sendFile() with pjax

I have observed that when I execute the function:
Yii::$app->request->sendFile() within a row with a gridView, instead of launching the file, it shows it embedded in the HTML.
Then if I remove the Pjax::begin() and Pjax::end() borders that enclose the GridView, Then download works.
How can I work with both functionalities without losing one of them?
This was discussed at Yii2 the solution for now is to use this method:
<?php Pjax::begin([
'id' => 'list',
'linkSelector' => '#list a:not([data-pjax=0])'
]); ?>
custom js or simple link to your action with download
pjax link
<?php Pjax::end(); ?>
It looks like this feature may be included in future releases.

How to add post ID to JSON URL?

I am not familiar with PHP Syntax. Now i added JSON Plugin to my wordpress website and activate it.
When i open url to get_recent_post , it's fine. JSON data are showing.
But when i open get_post , it's only showing
{"status":"error","error":"Include 'id' or 'slug' var in your request."}
So i don't know how to add post id to that URL.
here is the pic. Another get_page , gate_date_posts, etc... links are same showing error.
How can i do it?
To display a post's ID, simply use the following PHP code within the WordPress loop:
<?php the_ID(); ?>
To return the ID, use the following PHP code within the WordPress loop:
<?php get_the_ID; ?>
To get a post's id outside of the WordPress loop use the following PHP code:
<?php global $post; $post->ID ?>
Hope that helps you.
http://yoursite.com/api/get_posts/
with the s
If you use the WordPress link and click on it, you will be fine.

Saving page as .html and upload to ftp

I allow myself to ask the question here cause I looked for what to do regarding my problem but didn't find anything helpful.
Im having a webpage where users can modify information such as text, images etc, and I would like to add a button which would save the page as an .html in its current state (reloading the pas reset everything) and upload it on a ftp so they could have access to it online.
At this point, I know how to get all the html content using js .innerHTML, and how to send a file to the ftp using php, but I don't know what to look for to generate the .html file with the innerHTML content in it.
So here I am, asking for your help to guide me toward the right direction.
Thanks a lot!
Following theCNG27's suggestion, I think now the issue comes from my php script.. Sorry about that but Im really not used to php yet.
here is the php script:
<?php
$handle = fopen("filename.html", "w+");
fwrite($handle, $_POST['HTML']);
$dossier = 'endpoint/';
$fichier = basename($_FILES['HTML']['name']);
if(!isset($erreur))
{
$fichier = strtr($fichier,
'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$fichier = preg_replace('/([^.a-z0-9]+)/i', '-', $fichier);
if(move_uploaded_file($_FILES['HTML']['tmp_name'], $dossier . $fichier))
echo 'Upload done !';
}
else
{
echo 'Upload failed !';
}
}
else
{
echo $erreur;
}
?>
it says "no data received"
thanks a lot for the help
So you have Text (in your case the HTML code) in your javascript that you want to transfer to your php script and save it as a file!? Is that correct? If so, I would recommend this:
Since javascript is client-side and php is server-side you should send by POST.
I would use a form like this:
<form name="myform" action="url/to/php/script.php" method="POST">
<button type="submit">Submit!</button>
<input type="hidden" name="HTML" id="htmlsubmit" value="">
</div>
</form>
Now in the js-script you have to update the value of your hidden input like this:
document.getElementById("htmlsubmit").value = htmlcode;
htmlcode represents your HTML code you got through .innerHTML
Now in your PHP script you just have to receive the code and write it to a file:
$handle = fopen("filename.html", "w+");
fwrite($handle, $_POST['HTML']);
Since you already know how to upload a file to ftp via php, just do that with your file "filename.html" ;)
Hope that helps
EDIT: You edited your question and said you need help with the php script... This would be a possible complete script you could use:
<?php
$fname = "filename.html";
if(!isset($_POST['HTML']))
{
echo "Error: No data received...";
}else{
$handle = fopen($fname, "w+");
fwrite($handle, $_POST['HTML']);
fclose($handle);
echo "HTML Code written to file: ".$fname."";
}
?>
Is that all you need in your script?
Using JavaScript, calling innerHTML on the document element will give you the markup of the page as a string, including changes made to the DOM.
var markup = document.documentElement.innerHTML;
Then, you could post this markup to an endpoint using AJAX. The endpoint can receive the string, save it as a html file and then upload it somewhere via FTP.

Load div last after page load

Ok So I have a div that loads some data and makes the entire page load slowly. Fortunately this data can wait and I would like to load it last. I've seen the other posts like this but need a bit more help getting it to work as I am fairly inexperienced.
So for example I have:
<div id="stuff">
<?php Some PHP Here ?>
</div>
Now I have
$(window).load(function () {
$('#stuff').load('');
});
A few questions...Do I create an empty div and then fill it with the data or can I have the data in there and then specify that to load last, which would be preferred if possible.
I understand I can have a webpage loaded like $('#stuff').load('stuff.php'); but I use a variable inside the div so how would I pass it to that load function and into the php page?
I just don't know how to pass variables up to the javascript everytime that div is loaded as it's in a while loop.
Thanks for the help.
Because javascript processes on client side and PHP runs on server side, you cannot do PHP operations on HTML that have already been loaded (page has already been sent from server to client, so it is not reachable by PHP anymore).
Bacause of this fact you are going to need javascript AJAX for this.
Because you are beginner I suggest you use an ajax function from jQuery javascript framework for this matter. It works the same as javascript function and handles many operations automatically.
As you correctly presumed, you can first display your empty div and then, ... after page is fully loaded, you can call your ajax.
This ajax basically asks PHP script to provide loaded page with data that will be displayed in specified html element.
Code should look like this:
Include this to your PHP file with content that you load:
<script type="text/javascript">http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js</script> <!--loads jquery framework-->
<script type="text/javascript">
$(document).ready(function() {
//when page is loaded
$.ajax({
url: "ajax.php" //script with your data selection
}).done(function(data) { //when ajax call success
$("#ajax-element").html(data); //insert data returned from ajax.php into div with id ajax-element
});
});
</script>
<div id="ajax-element"></div>
This is PHP ajax file that will return data for you (just echo everything that you want to have in your "ajax-element" div, for example:
<?php
echo "some text";
?>
For more info you can find jquery AJAX specification here.

how to get data from an html5 form without using php in phonegap

have to design an app in phonegap using html5 in which some data has to be collected from the form and is stored in a database.
the question is that how can i get that data from the form without using php and the 'post' method..??
suppose the user has to login before proceeding, now how can i get the username and password from the form and match it to the database. The problem is not of
Phonegap is full javascript api. You won't have a system with html form sending request/response.
You have to design your app in a way it is using services. Try to add some javascript library like jquery mobile, sencha or just jquery (like example below) to help you in your work.
These libraries work fine with Phonegap (and sencha is the most optimized).
Write your service side ; it could be just simple php pages that will handle form parameters like a standard html application. You need a server side somewhere to access the database ; so host this file on an php/apache somewhere it can be accessible to the application.
--
<?php
/* mySaveService.php */
/* do something like $_GET['param1'] mysql_connect() / mysql_query() and all the stuff */
?>
--
Design your client's view side, you can write the bare html code or use a framework like Sencha (cleaner and harder way).
--
<form>
<input type="text" name="param1" value="" />
<input type="button" class="submit" value="send" />
</form>
--
Add a listener on your form submission or the submit-button click :
--
$('form .submit').click( function() {
saveInDb($(this).parents('form:first'));
});
--
Write a javascript function which sends an ajax call with your form's data. The url will not be in the same domain, be sure to allow cross-domain requests.
--
saveInDb = function($form) {
var myParams = array();
$form.find('input').each(function() {
myParams[$(this).attr('name')] = $(this).val();
});
$.ajax({
url: 'http://domain.com/mySaveService.php',
data: myParams,
success: myCallback
});
}
--
Don't forget to write the callback function, to alert the user that
everything went fine
--
myCallback = function() {
alert('It works');
}
--
Alternatively, if you don't need to gather all the form's data in one
place, you could use directly localstorage.
I hope it'll help. Regards