Reputation: 573
I have a script with a variable, this variable I am passing to a .php script and then from the .php script to a .txt file which then stores the number value from the variable, only problem is that when I pass the variable to the .txt file, instead of passing the number of the variable it just passes the name of it.
Here are my scripts:
php:
<?php
$variableToPass= $_POST['variableToPass'];
$filename = "textFilePass.txt";
$content = file_get_contents($filename);
$content .= $variableToPass. PHP_EOL;
file_put_contents($filename, $content);
?>
html:
<form id="payment-form" action="chargeCard.php" method="POST" name="payment-form">
<input type="text" name="amount" id="amount" />
<input type="hidden" id="variableToPass" name="variableToPass" value="variableToPass"/>
<input type="image" src="butImg.png" id="Button" value="but" alt="but"/>
</form>
<script type="text/javascript">
var variableToPass= 1;
document.getElementById("variableToPass").innerHTML = variableToPass;
document.getElementByID("variableToPass").value = variableToPass;
</script>
Upvotes: 4
Views: 2470
Reputation: 74217
"It is because I want to save the variable to be able to show it to all users on the website, so to do this I have the default value which is 1 and then I have a text input, which only allows numbers which are the variable+1 so lets say the default is 1, then I will put 2 into the input and then the .php file will get this input of 2 and save it into the .txt file, where after that the variable in the javascript will get this variable in the .txt file, and turn the new value into the variable. A bit complicated but basically it is just to save the variable for all users and not locally."
Ok, I have pretty good idea as to what you want to do here.
Important sidenote: Please go over my answer very carefully and in its entirety; I may not have fully grasped what you want to achieve here, so there are two options in my answer.
Here's what you need to do:
First check to see if the input is numeric by using is_numeric()
.
Then assign variables and add the POST amount with the $_POST['variableToPass']
array.
PHP
<?php
if(isset($_POST['amount']) && is_numeric($_POST['amount']) ){
$amount = $_POST['amount'];
}
else{
echo "It is not numeric. Please click back and enter an integer.";
exit;
}
$variableToPass1 = $_POST['variableToPass'];
$variableToPass = $variableToPass1 + $amount;
$filename = "textFilePass.txt"; // make sure this file exists before executing
$content = file_get_contents($filename);
$content .= $variableToPass. PHP_EOL;
file_put_contents($filename, $content);
HTML form
<form id="payment-form" action="chargeCard.php" method="POST" name="payment-form">
<input type="text" name="amount" id="amount" />
<input type="hidden" id="variableToPass" name="variableToPass" value="variableToPass"/>
<input type="submit" name="submit" value="Submit"/>
</form>
<script type="text/javascript">
var variableToPass= 1;
document.getElementById("variableToPass").innerHTML = variableToPass;
document.getElementById("variableToPass").value = variableToPass;
</script>
However, using this method will keep adding/appending to the file. If this isn't the desired result, you will need to remove the dot in:
$content .= $variableToPass. PHP_EOL;
^
to read as:
$content = $variableToPass. PHP_EOL;
Plus, as already stated; this line:
document.getElementByID("variableToPass").value = variableToPass;
should read as:
document.getElementById("variableToPass").value = variableToPass;
Option 2 Using the number from the "amount" input to write to file.
This will write whatever number from the input and write it to file, and not appending to it.
If a number is not inserted and by clicking the submit button only, will write the number you've defined in var variableToPass= 1;
<?php
if(isset($_POST['amount']) && is_numeric($_POST['amount']) ){
$variableToPass = $_POST['amount'];
}
else{
$variableToPass = $_POST['variableToPass'];
}
$filename = "textFilePass.txt";
$content = file_get_contents($filename);
$content = $variableToPass. PHP_EOL;
file_put_contents($filename, $content);
Footnotes:
Upvotes: 1
Reputation: 692
Try this:
$var_str = var_export($text, true);
$var = "<?php\n\n\$$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
Upvotes: 1
Reputation: 319
<input type="hidden" id="variableToPass" name="variableToPass" value="variableToPass"/>
This will always send the value (value="variableToPass") try changing it to (value="1") this should return 1
Upvotes: 1
Reputation: 9430
You have a typo here:
document.getElementByID("variableToPass").value = variableToPass;
^
change it to:
document.getElementById("variableToPass").value = variableToPass;
The value doesn't get changed and stays with the default value, being variableToPass
, which coincides with the PHP variable name.
Besides you don't need to change innerHTML - it is input, it has its value.
Upvotes: 3