Reputation: 541
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.
<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
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) {
}
?>
Upvotes: 1
Views: 102
Reputation: 1
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);
Upvotes: 0
Reputation: 692
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=""/>
Upvotes: 1