1

I am having some trouble figuring out a way to pass a javascripts value to a .php script, and then from there to a .txt script.

When doing this with a regular number it works, but when I want to do it with the variable which it has to be, the .txt file is left blank and nothing has been added to the file. I have searched the web "dry" for options, and I just can't figure out how to make it work. currently my scripts look like below and

I believe that is the right way of doing it, there is just some problem with it as I said, and as I also said I believe the mistake is in the part from the javascript file to the .php file.

javascript/html:

<form id="pg-form" action="chargeCard.php" method="POST" name="pg-form">
<input onkeypress="return isNumberKey(event)" type="text" name="amount" id="amount" />
<input type="hidden" id="curValueField" value=""/> <!-- this is where I am trying to pass the value -->
<input type="image"  src="pgBut1.png" id="pgBut" value="submit" alt="butPG"/>
</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>

<script type="text/javascript"> 
    window.onload(function(){
    var currentVarValue = 1; //this is the variable value I am trying to pass
    document.getElementById("currentVarValue").innerHTML = currentVarValue;
    document.getElementById("curValueField").value = currentVarValue;   //this is where I get the variable value and make the new id to get in the html form        
});
</script>

php:

<?php 

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

    \Stripe\Stripe::setApiKey("removed for security");

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

    $curValue= $_POST['curValueField']; //this is where I try to get the variable value

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

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

    //pass value to .txt file start
    $filename = "getVarValue.txt";
    $content = file_get_contents($filename);
    $content .= $curValue;
    file_put_contents($filename, $content);
    //pass value to .txt file end       

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

?>

2 Answers 2

1

Your curValueField doesn't have a name, so its value is never sent to your PHP.

Try this :

<input type="hidden" id="curValueField" name="curValueField" value=""/>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer, I have figured out how to fix it.
0

I think you forgot to "name", try

<input type="hidden" id="curValueField" name="curValueField" value=""/>

And to .txt, can try below code

$fp = fopen("test.txt", "a"); 
if($fp) { 
    fwrite($fp,$msg);
}   
fclose($fp);

2 Comments

That has been suggested already. And the OP's file writing code looks ok, doesn't it?
"When doing this with a regular number it works" look like it works. But it can be interesting for the OP.

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.