1

In the below script an input which holds a token value is generated once the user has click the submit button and where no errors are present. Then I want to post that value inside the php page so that I can use it in my query. Here the input value is generated successfully but posting its value to the php page and executing the php page is the issue (i elaborate a bit below) below is the script:

 <script type="text/javascript">
    // This identifies your website in the createToken call below
  Stripe.setPublishableKey('code');

    var appendedStripeToken = false;

var stripeResponseHandler = function(status, response) {
    var $form = $('#payment-form');

    if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
                $form.find('button').prop('disabled', false);

    } else {
        // token contains id, last4, and card type
        var token = response.id;
        handleCall(token);
    }
};

function handleCall(token) { 
   var $form = $('#payment-form');
    if (!appendedStripeToken) { 
        // Insert the token into the form so it gets submitted to the server
$form.append($('<input type="text" name="stripeToken" />').val(token));
        appendedStripeToken = true; 
        phpCall(); 

    } 
}

function onSubmit() {
    var $form = $('#payment-form'); // TODO: give your html-form-tag an "id" attribute and type this id in this line. IMPORTANT: Don't replace the '#'!

    // Disable the submit button to prevent repeated clicks
  // TODO: give your html-submit-input-tag an "id" attribute

    Stripe.card.createToken($form, stripeResponseHandler);
}


function phpCall() {
 if( appendedStripeToken === true ){
   $.ajax({
    type: "POST",
    data: {run: true},
    url: 'functions/paymentEmail.php',
    success: function (response) {//response is value returned from php (for    your example it's "bye bye"
                  $('#payment-form').prop('disabled', true); // TODO: give your html-submit-input-tag an "id" attribute

        alert(response);
    }
   });
 }
} 
  </script>

Now take a particular look at this part

url: 'functions/paymentEmail.php',
    success: function (response) {//response is value returned from php (for    your example it's "bye bye"
                  $('#payment-form').prop('disabled', true); // TODO: give your html-submit-input-tag an "id" attribute

        alert(response);

I do not want the php function to be executed with an alert box, because it will not run. enter image description here

below is the php code:

     <?php


        $course_price_final = $_POST['course_price_final'];
        $course_token = $_POST['stripeToken'];
        $course_provider = $_POST['course_provider'];
        $user_email = $_POST['user_email'];
        $course_delivery = $_POST['course_delivery'];
        $order_date = date("Y-m-d");
        $insert_c = "insert into orders (course_title,course_price_final,course_provider,user_email,course_date,course_delivery,order_date,course_token) 
                 values ('$crs_title','$course_price_final','$course_provider','$user_email','$course_date1','$course_delivery','$order_date','$course_token')";
        $run_c = mysqli_query($con, $insert_c);

        $location = "../paymentConfirmed.php";


    header( "Location: $location" );

?>
4
  • 1
    I think you are overusing AJAX. AJAX is needed to create dynamic pages, where some functionality is done on the server so you don't have to refresh the page. Why not just create a form and submit it? That way if it is successful, do a redirect. Your method just seems overkill. Commented Apr 17, 2015 at 21:30
  • I know its various complications that led me to this situation. I could elaborate for paragraphs on why but right im just trying to move forward Commented Apr 17, 2015 at 21:35
  • So you want to display the response from the redirect? Well, you can probably insert all of that HTML into the document, overriding the old one. If you care about the url that is displayed, you can either use history.pushState(null, null, xhr.responseURL) in browsers that support both history.pushState and the responseURL property, I don't know how to get the responseURL property with jQuery though (if it's not possible, you'd have to use vanilla JS for that). Or like the answer suggested, you can use window.location = response, but you'd have to return the actual URL in your PHP script. Commented Apr 17, 2015 at 21:44
  • Also, instead of using the XHR object responseURL, you can hard-code the url that the script redirects to into history.pushState. Just an idea, but that's probably over-complicating everything a little bit more. Commented Apr 17, 2015 at 21:51

1 Answer 1

4

If you want to make a redirect using ajax you can return a url, than make a window.location = response

Sign up to request clarification or add additional context in comments.

17 Comments

This kind of beats the purpose of using AJAX
thanks for your responses. it however does not seem to work properly: it just translate the world document and add it as such the url<!DOCTYPE%20html><html><head><link%20rel="shortcurt%20icon"%20href="content/icon.png">%20%20%20%20%20%20%20%20%20%20<link%20href="cssPaper/bootstrap.min.css"%20rel="stylesheet"%20media="screen">%20%20%20%20%20%20%20%20<link%20href="css/mystyle.css"%20rel="stylesheet"%20med
Remove it header( "Location: $location" ); from your file and return a valid url
what do you mean a valid url?
You must return $location I see it's your url
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.