Sm00rf9000
Sm00rf9000

Reputation: 541

How to increment a javascript variable from a .php script

So i'm in a bit of a dilemma, I have a script where I am trying to make a javascript variable increment by 1 when a special function is called. The problem is that this incrementian, can only happen if another function is happening in a .php script, as I want to make sure it only happens when this function is called. Now my problem/dilemma is I know I am able to take this javascript variable and send it to the .php script and then access it this way, there is just two ways of doing this, either making a function in the javascript and then call that function through the .php script, or access the javascript variable and then do the incrementing through the .php script. What would be the best way, passing the variable or the function and how can this be done? I have a "POST" form which submits a couple of other values to the .php script, and I know you can use the form to pass them but again I am not sure how.

So far my scripts look like below.

html/javascript:

<!DOCTYPE html>
<html>

<head>
</head>

<body>

                <div class="myVar">
                    myVar: $<span id="myVar"></span>
                </div>

                <script type="text/javascript"> 

                    var myVar= 1; // this is the variable I want to do the incremention to
                    document.getElementById("myVar").innerHTML = myVar;
                    document.getElementByID("myVar").value = myVar;

                    /*
                    function addOne() {
                        myVar= myVar+1;
                        document.getElementById("myVar").innerHTML = myVar;
                    }
                */
                </script>

                <!-- FORM FOR SUBMIT START-->

                    <form id="payment-form" action="chargeCard.php" method="POST" name="payment-form">
                    <input onkeypress="return isNumberKey(event)" type="text" name="amount" id="amount" />
                    <input type="image"  src="BuyButton1.png" id="customButton" value="pay" alt="button"/>
                    </form>

                    <script type="text/javascript">
                        function isNumberKey(evt)
                    {
                    var charCode = (evt.which) ? evt.which : event.keyCode
                    if (charCode > 31 && (charCode < 48 || charCode > 57))
                    return false;

                    return true;
                    }
                    </script>

                 <!--FORM FOR SUBMIT ENDING-->
</body>

</html>

php:

<?php 

    require_once('./stripe-php/init.php');

    \Stripe\Stripe::setApiKey("secret code removed");

    $token = $_POST['stripeToken'];
    $myAmount = $_POST['amount'];
    $describtion = $_POST['description'];

    $myAmount = round((int)$myAmount*100,0);

    try {
    $charge = \Stripe\Charge::create(array(
    "amount" => $myAmount, // amount in cents, again
    "currency" => "usd",
    "source" => $token,
    "description" => $describtion));

    } catch(\Stripe\Error\Card $e) {
    }

?>

Upvotes: 1

Views: 479

Answers (2)

KJ Price
KJ Price

Reputation: 5984

I would probably use a hidden input in your form instead of using a variable:

<?php
   $myVar = intval($_POST['myVar'] ? $_POST['myVar'] : 0) + 1;
   //other PHP stuff
?>

And then add this to your form:

<input type="hidden" id="myVar" name="myVar" value="<?= $myVar?> />

And then access the hidden field with Javascript:

var myVar = document.getElementById('myVar').value;

Upvotes: 1

Kevin Grabher
Kevin Grabher

Reputation: 399

You could use an ajax function (js) to call your php function, and use it's output to be fed back into your javascript.

A bit of jQuery code to give you an idea how this could work.

$.ajax({
  url: "http://example.com/somescript?value=1"
})
  .done(function( data ) {
      alert('php script returned: ' + data);
  });

Upvotes: 1

Related Questions