1

So in an earlier part of my code, within tags I set the value of variable charge to a different link.

How do I use the value of this variable in the later javascript as 'url: charge' does not work. Do I need to write the variable differently.

'Url: chargesilver.php' works, but 'url: charge' doesn't.

<script>

function setmembershipayment(){
    if (usermembershipchoice == "Silver") {
        var charge = "chargesilver.php";
        alert(charge);
    } else if (usermembershipchoice == "Gold") {
        var charge = "chargegold.php";
        alert(charge);
    } else if (usermembershipchoice == "Platinum") {
        var charge = "chargeplatinum.php";
        alert(charge);
    } else { 
       alert("ERROR");
    } // THE MISSED BRACKED!! ;)
}
</script>

.

<script>
    $.ajax({
                  type: "POST",
                  url: charge,
                  data: form.serialize(),
                  success: function(response){
 </script>
4
  • 1
    Just curious, why are these in separate script tags? Commented Jan 16, 2019 at 21:42
  • The 2nd bit of javascript is at the bottom of the page and was coded by someone else as it is part of the stripe payments integration. 1st part is at the top and something I've just coded in relation to buttons at the top of the page. Commented Jan 16, 2019 at 21:43
  • Where's usermembershipchoice coming from? Commented Jan 16, 2019 at 21:45
  • It's value depends on the button the user clicks on. Eg. select Silver, usermembershipchoice = 'silver' Commented Jan 16, 2019 at 21:47

3 Answers 3

1

The easiest way is to make your function return the charge you want to use.

function getMembershipPayment(){
    if (usermembershipchoice == "Silver") {
        return "chargesilver.php";
    } else if (usermembershipchoice == "Gold") {
        return "chargegold.php";
    } else if (usermembershipchoice == "Platinum") {
        return "chargeplatinum.php";
    } else { 
       alert("ERROR");
       return "some default value"
    } // THE MISSED BRACKED!! ;)
}

$.ajax({
  type: "POST",
  url: getMembershipPayment() // returns the value of charge,
  data: form.serialize(),
  success: function(response){}
})
Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU, wont let me accept your answer yet for some reason
0

Perhaps call the function from url to return the correct string value. And perhaps use a switch instead of an if/else structure as it's a little easier to read.

function setmembershipayment(usermembershipchoice) {
  switch (usermembershipchoice) {  
    case 'Silver': return "chargesilver.php";
    case 'Gold': return "chargegold.php";
    case 'Platinum': return "chargeplatinum.php";
    default: console.log('error'); break;
  }
}

$.ajax({
  type: "POST",
  url: setmembershipayment(usermembershipchoice),
  data: form.serialize(),
  success: function(response) {
    ...
  }
});

Comments

0

charge is not global.

Its scope is just within the function

Declare it outside the function

var charge = ''

function setmembershipayment(){
    ....

Sample

<script>

var charge = ''

function setmembershipayment(){
  if (usermembershipchoice == "Silver") {
    charge = "chargesilver.php";
    alert(charge);
  } else if (usermembershipchoice == "Gold") {
    charge = "chargegold.php";
    alert(charge);
  } else if (usermembershipchoice == "Platinum") {
    charge = "chargeplatinum.php";
    alert(charge);
  } else { 
   alert("ERROR");
 } // THE MISSED BRACKED!! ;)
}

 setmembershipayment()

 $.ajax({
     type: "POST",
     url: charge, // OR Just return it from the function here and make sure there is a default value
     data: form.serialize(),
     success: function(response){
</script>

OR Just return it from the function and make sure there is a default value

7 Comments

But how do I declare it outside the function, because it's value is dependent on and decided by the function. Can't I just make it global?
same way you are using 'usermembershipchoice'. You could also return it from the function and use the reurn value in your ajax call
<script> var charge; function setmembershipayment(){ if (usermembershipchoice == "Silver") { var charge = "chargesilver.php"; alert(charge); } else if (usermembershipchoice == "Gold") { var charge = "chargegold.php"; alert(charge); } else if (usermembershipchoice == "Platinum") { var charge = "chargeplatinum.php"; alert(charge); } else { alert("ERROR"); } // THE MISSED BRACKED!! ;) } </script>
this would work but would leave you vulnerable to tampering. Example, if platinum members get better discounts, it is trivial to open the console and set your charge value to Platinum
unction getMembershipPayment(){ if (usermembershipchoice == "Silver") { return "chargesilver.php"; } else if (usermembershipchoice == "Gold") { return "chargegold.php"; } else if (usermembershipchoice == "Platinum") { return "chargeplatinum.php"; } else { alert("ERROR"); return "some default value" } // THE MISSED BRACKED!! ;) } $.ajax({ type: "POST", url: getMembershipPayment() // returns the value of charge, data: form.serialize(), success: function(response){} })
|

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.