【问题标题】:Pass variable to .txt file将变量传递给 .txt 文件
【发布时间】:2015-04-01 16:45:46
【问题描述】:

我正在尝试将变量从 html 表单传递到 .php 脚本,然后从 .php 脚本传递到存储变量值的 .txt 文件中。问题是,当我测试脚本而不是发布变量编号时,它只是发布变量名称“numberVariable”。

html/javascript

    <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="hidden" id="numberVariable" name="numberVariable" value="numberVariable"/> <!--this is where I try to pass the variable to the .php script-->
                            <input type="image"  src="Button1.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>

<script type="text/javascript"> 

                            var numberVariable= 1; //this is the variable I am trying to pass to the .php script to then pass the value to the .txt file

                            document.getElementById("numberVariable").innerHTML = numberVariable;
                            document.getElementByID("numberVariable").value = numberVariable;

                        </script>

php

<?php 

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

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

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

    $numberVariable= $_POST['numberVariable']; //this is where I get the variable from the form

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

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

    //PASTE VARIABLE TO .TXT FILE START

    $filename = "iPhoneAuctionBids.txt";
    $content = file_get_contents($filename);
    $content .= $numberVariable. PHP_EOL;
    file_put_contents($filename, $content);

    //PASTE VARIABLE TO .TXT FILE END

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

?>

【问题讨论】:

  • 首先你必须在html元素之后移动javascript代码。因为在执行它们时,numberVariable 不存在。
  • 谢谢你的信息,我已经按照你说的做了。

标签: javascript php html text-files


【解决方案1】:

你不会对文件做任何事情。

您需要打开文件并将该值写入其中。

$filename = "iPhoneAuctionBids.txt"; 
$content = file_get_contents($filename);
$content .= $numberVariable . PHP_EOL;
file_put_contents($filename, $content);

此外,您可能希望在对 DOM 进行任何 js 处理之前等待生成 html。把你的JS代码放进去

window.onload(function(){
// here
});

【讨论】:

  • 感谢您的回答,我已将我的脚本编辑为如下链接中的样子,但现在提交时,它可以工作,但它只是写变量“numberVariable”的名称而不是数字pastie.org/private/mwoigoc208pzjpeffqfg
  • 你能发一个链接到你的 JavaScript 吗?
  • 我尝试将 javascript 编辑为如下链接中的样子,但问题是它应该可以在没有在输入字段中输入值的情况下工作,然后在 javascript 函数中将值分配到外部,但它对我不起作用,但是如果我然后选择将 1 放在值点中它起作用,所以我将变量传递给 .php 的方式一定有问题 php:pastie.org/private/fbzm92prvem9mpvuscgzq javscript:pastie.org/private/no4a224edvqbyubhdsixuq#20跨度>
  • 正如我在回答中所写,您需要将您的 JS 封装在 window.onload 中,否则输入可能无法让您在其中输入值。比如:window.onload(function(){ document.getElementById("numberVariable").value = numberVariable ; })。您不需要用于输入字段的 innerHTML 部分。
  • 非常感谢您的回答,您的做法是对的,我刚刚编辑了一下。
【解决方案2】:

您必须实际打开文件并将新数据写入其中。您所做的只是使用文件名设置本地变量,而不是打开它,也不获取内容。 http://php.net/manual/en/function.fopen.php 是您需要先阅读的内容。

【讨论】:

    猜你喜欢
    • 2014-09-30
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多