Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试将输入文本字段传递并捕获到.php文件以写入.txt文件时,我做错了什么?测试代码如下:_Php_Forms_Variables_Capture - Fatal编程技术网

尝试将输入文本字段传递并捕获到.php文件以写入.txt文件时,我做错了什么?测试代码如下:

尝试将输入文本字段传递并捕获到.php文件以写入.txt文件时,我做错了什么?测试代码如下:,php,forms,variables,capture,Php,Forms,Variables,Capture,我的测试代码如下: 这两个文件都是.php。为了避免冲突,我已经编写了30年的程序,但是我对.js和.php都是新手,我不知道该怎么做才容易。我已经阅读并尝试了所有适用的例子,但它们对我来说都不起作用。请告诉我我在哪里搞砸了 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&g

我的测试代码如下: 这两个文件都是.php。为了避免冲突,我已经编写了30年的程序,但是我对.js和.php都是新手,我不知道该怎么做才容易。我已经阅读并尝试了所有适用的例子,但它们对我来说都不起作用。请告诉我我在哪里搞砸了

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

    <title>TestofTopicText</title>
    <script language="Javascript">
    <!--
    function OnButton1()
    {
    var newtopic = document.getElementById('topic');
    document.Form1.target = "_self";    
    document.Form1.action = "1-open-close.php?var=$newtopic";
    document.Form1.submit();             // Submit the page
    }
    -->
    </script>
    </head>

    <body>
    <h3><span style="color: #00ff00;">If NOT found to right ENTER Your Topic Here!        </span></h3>
    // Using get method as I read was appropriate for getElementById
    <form id="Form1" method="get" name="Form1">
  <input type="text" name="q" id="topic" size="55" />
      <input type="submit" name="sa" value="Search" onclick="OnButton1()"/>
    </form>
    </body>

    </html>

    // I am passing to this .php file known as 1-open-close.php
    // The file opens and writes test text but I can't get topic text from other file?
<?php
    $topic = $_GET['var'];
    $myFile = "Topics.txt";
    $fh = fopen($myFile, 'a') or die("can't open file");
    $stringData = "Test to Make Sure Open \n";
    fwrite($fh, $topic);
    fwrite($fh, $stringData);
    fclose($fh);
    // return true;
?>

文本测试
如果找不到,请在此处输入您的主题!
//在阅读时使用get方法适合于getElementById
//我将传递给这个名为1-open-close.php的.php文件
//文件打开并写入测试文本,但我无法从其他文件获取主题文本?

编辑:我看到您将表单操作放入javascript中。真奇怪。无论如何:

我建议您使用大量echo语句来找出变量的值

有用的东西:

print_r($_POST); // find out what values have been received by POST method
print_r($_SESSION); // find out what values are stored in SESSION

标记将那些
print\r
包围起来,使它们易于阅读。

不确定您是否真的希望将javascript包含在其中,但您可以通过使用html固有的表单行为来简化,并引用传入的输入。i、 e.您已经将表单指定为GET方法,因此表单标记中包含的输入类型文本将被发布,无需任何额外的工作。在php端,您可以通过使用html输入中指定的“name”作为数组的索引来引用该值。希望这有帮助

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

    <title>TestofTopicText</title>
    </head>

    <body>
    <h3><span style="color: #00ff00;">If NOT found to right ENTER Your Topic Here!        </span></h3>
    // Using get method as I read was appropriate for getElementById
    <form id="Form1" method="get" name="Form1" action="1-open-close.php">
  <input type="text" name="q" id="topic" size="55" />
      <input type="submit" name="sa" value="Search"/>
    </form>
    </body>

    </html>

    // I am passing to this .php file known as 1-open-close.php
    // The file opens and writes test text but I can't get topic text from other file?
<?php
    $topic = $_GET['q'];
    $myFile = "Topics.txt";
    $fh = fopen($myFile, 'a') or die("can't open file");
    $stringData = "Test to Make Sure Open \n";
    fwrite($fh, $topic);
    fwrite($fh, $stringData);
    fclose($fh);
    // return true;
?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>TestofTopicText</title>
</head>
<body>
  <h3 style="color:#0f0;">If NOT found to right ENTER your topic here!</h3>
  <form method="post" action="1-open-close.php">
    <input type="text" name="q" size="55" />
    <input type="submit" value="Search" />
  </form>
</body>
</html>

文本测试
如果找不到,请在此处输入您的主题!
//在阅读时使用get方法适合于getElementById
//我将传递给这个名为1-open-close.php的.php文件
//文件打开并写入测试文本,但我无法从其他文件获取主题文本?

JavaScript不像PHP那样插入变量,因此它实际上发送的是字符串
$newtopic
,而不是您想要的。无论如何,整个事情都是多余的。试试这个:

<?php
$topic = $_POST['q'];
$myFile = "Topics.txt";
$fh = fopen($myFile,"a") or die("can't open file");
$stringData = "Test to make sure open\n";
fwrite($fh,$topic);
fwrite($fh,$stringData);
fclose($fh);
?>

文本测试
如果找不到,请在此处输入您的主题!
那么您的PHP文件应该是:

var newtopic = document.getElementById('topic');
document.Form1.target = "_self";    
document.Form1.action = "1-open-close.php?var=$newtopic";
此处:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

    <title>TestofTopicText</title>

    </head>

    <body>
    <h3><span style="color: #00ff00;">If NOT found to right ENTER Your Topic Here!        </span></h3>
    // Using get method as I read was appropriate for getElementById
    <form id="Form1" method="get" name="Form1" action="1-open-close.php">
  <input type="text" name="q" id="topic" size="55" />
      <input type="submit" name="sa" value="Search" />
    </form>
    </body>

    </html>

    // I am passing to this .php file known as 1-open-close.php
    // The file opens and writes test text but I can't get topic text from other file?
<?php
    $topic = $_GET['q'];
    $myFile = "Topics.txt";
    $fh = fopen($myFile, 'a') or die("can't open file");
    $stringData = "Test to Make Sure Open \n";
    fwrite($fh, $topic);
    fwrite($fh, $stringData);
    fclose($fh);
    // return true;
?>
您试图用$newtopic传递JavaScript变量newtopic(这是PHP变量的语法,在这里不适用)。 您还试图发送元素本身,这是没有意义的。 输入框有一个name=“q”,这就是您应该在PHP中访问的内容

尽管如此,代码的格式仍然很糟糕。在发送“q”之前尝试访问它会导致错误

我在表单中添加了一个操作,并删除了不必要的脚本。我还将
$topic=$\u GET['var']
更改为
$topic=$\u GET['q']


文本测试
如果找不到,请在此处输入您的主题!
//在阅读时使用get方法适合于getElementById
//我将传递给这个名为1-open-close.php的.php文件
//文件打开并写入测试文本,但我无法从其他文件获取主题文本?

在什么方面不起作用?好吧,总是迟到。。。Benjamin Powers和Kolink首先给出了解决方案。谢谢,它与“q”一起工作。我像以前一样对它进行了编码,因为我的实际应用程序需要使用不同类型的方法和操作执行多个操作。你能解释一下为什么输入时使用Name和Id有什么区别吗?如果我添加一个If变量!='',这是确保变量“q”不为空的正确语法吗?我想@BenjaminPowers已经解释了名称与id部分的区别。关于变量是否为空,您只需要添加一个if来测试变量是否已设置:if(isset($\u GET['q']){…打开并写入文件…}我还建议您在打开和写入文件时进行进一步的测试。请看,谢谢,它与“q”一起工作。我像以前一样对它进行了编码,因为我的实际应用程序需要使用不同类型的方法和操作执行多个操作。你能解释一下为什么输入时使用Name和Id有什么区别吗?如果我添加一个If变量!='',这是确保变量“q”不为空的正确语法吗?在原始代码中,您对变量newtopic的引用有点不正确。(您在php中使用变量前面的美元符号,并将其包含在字符串中)对于javascript中的字符串连接,您需要执行如下操作:document.Form1.action=“1-open-close.php?var=“+newtopic;而且。。。关于你关于身份证和姓名的问题。唯一的区别是html表单在提交时会以本机方式使用“name”属性。因为你用javascript绕过了它,所以它并不会真正影响它。。。通过使用“submit”类型的输入,您可能会遇到表单可以使用本机html功能提交的问题。为了避免这种情况,您可以将其更改为“按钮”类型,即。