-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

Pearly
  • 1
  • 1
  • You could just concatenate it: `'successful.php?successfullypaid=' + referenced + '&...` – brombeer Oct 19 '21 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 ... – CBroe Oct 19 '21 at 13:58
  • Constant [FILTER_SANITIZE_STRING](https://stackoverflow.com/questions/69207368/constant-filter-sanitize-string-is-deprecated) is deprecated. Please stop using it. – Dharman Oct 25 '21 at 21:50

1 Answers1

0

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; ?>`;
naxsi
  • 602
  • 3
  • 15
  • 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; ?>`; – Pearly Oct 19 '21 at 16:00
  • this not work? it should work. – naxsi Oct 19 '21 at 16:01