-1

I am trying to pass some PHP variables and Javascript variable name "referenced" into a URL in Javascript. Below are the PHP variables which are working very fine

//Collect user's data
$amt = $_GET['amount'];
$email = $_GET['email'];
$firstname = "bas";
$lastname = "aik";
<script>    
callback: function(response){
              const referenced = response.reference;
              window.location.href='successful.php?successfullypaid=referenced&email=<?php echo $email; ?>';
          },
<script?

I have pasted my entire code below for you to see.

    <?php
//sanitize form inputs
$sanitizer = filter_var_array($_POST, FILTER_SANITIZE_STRING);

//Collect user's data
$amt = $_GET['amount'];
$email = $_GET['email'];
$firstname = "bas";
$lastname = "aik";

//Make sure fields are not empty
//if(empty($amt)) {
//  header("Location: deposit.php");
//}else{


?>
                <div class="modal-body">
                    <p>Confirm Online payment to pay with paystack</p>
                    <form >
                        <script src="https://js.paystack.co/v1/inline.js"></script>
                        <button type="button" class="btn btn-success confirm-button" onclick="payWithPaystack()"> Confirm </button>
                        <a class="btn btn-danger" href="index.php"> Decline </a> 
                    </form>
                </div>
               <!-- <div class="modal-footer">
                    
                    <a href="index.php"> <button type="button" class="btn btn-primary">Go to Account</button></a>
                </div>-->
            </div>
        </div>
    </div>
</div>
<script>
  function payWithPaystack(){
    const api = "pk_test_71bd9a7489bee5e300594cabeac43344eed2ef5c";
    var handler = PaystackPop.setup({
      key: api,
      email: '<?php echo $email; ?>',
      amount: <?php echo $amt*100; ?>,
      currency: "NGN",
      ref: ''+Math.floor((Math.random() * 1000000000) + 1), // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
      /*firstname: <?php echo $firstname; ?>,
      lastname: <?php echo $lastname; ?>,*/
      metadata: {
         custom_fields: [
            {
                /*display_name: <?php echo $firstname; ?>,
                variable_name: <?php echo $lastname; ?>,
                value: "+2348012345678"*/
            }
         ]
      },
      callback: function(response){
          const referenced = response.reference;
          window.location.href='successful.php?successfullypaid=referenced&email=<?php echo $email; ?>';
      },
      onClose: function(){
          alert('window closed');
      }
    });
    handler.openIframe();
  }
</script>

I have tried all the answers to this question How can i pass multiple parameters in location.href in php?, but they are not working for me.

window.location.href='successful.php?successfullypaid=referenced&email=<?php echo $email; ?>';

This is the only solution that is close to what I want, the PHP variable works fine but the javascript variable doesn't work

3
  • You could just concatenate it: 'successful.php?successfullypaid=' + referenced + '&... Commented Oct 19, 2021 at 12:11
  • Looks like your actual question boils down to the trivial topic of "how do I insert a variable into a string in JavaScript", and that one you could have easily researched on your own ... Commented Oct 19, 2021 at 13:58
  • Constant FILTER_SANITIZE_STRING is deprecated. Please stop using it. Commented Oct 25, 2021 at 21:50

1 Answer 1

1

try this, now you just making a string, different of PHP the javascript not allow you pass variables inside string.

window.location.href=`successful.php?successfullypaid=${referenced}&email=<?=$email; ?>`;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @naxsi, this works for me. But am trying to add more PHP variables like this window.location.href=successful.php?successfullypaid=${referenced}&email=<?=$email; ?>&amount=<?=$amount; ?>;
this not work? it should work.

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.