0

我的测试代码如下:两个文件都是 .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;
?>
4

4 回答 4

1

不确定您是否真的希望在其中涉及 javascript,但您可以通过仅使用 html 原生的表单行为并引用传入的输入来简化。即您已经将表单指定为方法 GET,因此输入类型表单标签中包含的文本将无需任何额外努力即可发布。在 php 方面,您可以通过使用 html 输入中指定的“名称”作为数组的索引来引用该值。希望这可以帮助!

<!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;
?>
于 2012-11-28T01:20:50.577 回答
1

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

<!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>

那么你的 PHP 文件应该是:

<?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);
?>
于 2012-11-28T01:21:08.980 回答
0

编辑:我看到您将表单操作放入您的 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用标签包围它们,<pre></pre>使它们易于阅读。

于 2012-11-28T01:19:07.137 回答
0

这里:

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

您正在尝试使用 $newtopic 传递 JavaScript 变量 newtopic(这是 PHP 变量的语法,不适用于此处)。而且您还试图发送元素本身,这没有意义。输入框有一个 name="q",这就是您应该在 PHP 中访问的内容。

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

我已经在表单中添加了一个操作,并删除了您不必要的脚本。我也$topic = $_GET['var']改成$topic = $_GET['q'];

  <!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;
?>
于 2012-11-28T01:27:38.330 回答