2013-08-20 36 views
0

最新编辑:事实证明,在formprocessing.php,isset($ _ POST ['submit1'])是FALSE(其中'submit1'现在是提交按钮的名称;原来它没有名字)。正如下面的代码所示,我最初并没有为此进行测试。PHP Post方法显然不工作

它确实解释了很多。但剩下的问题是为什么 isset($ _ POST ['submit1'])是FALSE。

注意:此问题已被编辑以反映更近的见解。

作为一名PHP初学者,我正在尝试将表单发布到服务器,并且它不起作用。在所有的可能性中,我忽略了一些简单的东西,但我只是没有看到它。

我有两个文件:'form.php'和'formprocessing.php',都位于同一个文件夹中。我已经包含了下面的完整代码,但首先要解释一下。文件'form.php'包含表单本身,包括'post'方法。文件'formprocessing.php'是'post'方法的目的地,也就是说,'action = formprocessing.php'。

想法是,formprocessing应该没有'formprocessing.php'加载或'form.php'重新加载(因此“event.preventDefault();”在form.php)。我知道我也可以发布到'form.php'本身,但现在我想用两个单独的文件去。

我已经从jQuery网站几乎字面上取了'form.php'的代码(http://api.jquery.com/jQuery.post/,最后一个例子)。我已将action属性中的文件名更改为'formprocessing.php',并添加了必要的JSON.stringify命令(请参阅下文)。

'formprocessing.php'中的其余代码只是简单地提取发布的输入字段的值(表单中只有一个字段),并以“form.php”的形式将其返回给JSON对象(然后在form.php中进行字符串化)。至少这是主意!

在'formprocessing.php。'生成的脚本 - 打字,说:“XXX”到输入字段后 - 如下:

<script> {"content":"xxx"} </script> 

现在我的眼里,这个剧本似乎是正确的 - 但果真如此吗?这是我现在想知道的第一件事情。

编辑:我现在已经删除了formprocessing.php中的所有HTML标记。这也意味着生成的脚本简化为{“content”:“xxx”},即只有JSON对象。虽然不改变结果。

因为有些事情后来出问题了。也就是说,在'form.php'结尾处,我将从'formprocessing.php'返回的数据内容附加到div元素。但事实上,连接到div的字符串变成“{”length“:0,”prevObject“:{”0“:{},”1“:{},”2“:{},”3“ :{} “4”:{}, “5”:{}, “6”:{}, “7”:{}, “8”:{}, “长度”:9}, “选择器”: “#content”}“ - 即指示长度为0的对象。而不是从'formprocessing.php'(正如我所说的也等于表单字段的原始输入)推测的实际数据。

这里来“form.php的”的代码:

<!doctype html> 
<html lang="en"> 

<head> 
    <meta charset="utf-8"> 
    <title>jQuery.post demo</title> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
</head> 

<body> 

    <form action="formprocessing.php" id="searchForm"> 
    <input type="text" name="s" placeholder="Search..." /> 
    <input type="submit" value="Search" /> 
    </form> 

    <!-- the result of the search will be rendered inside this div --> 
    <div id="result"></div> 

    <script> 

    /* attach a submit handler to the form */ 
    $("#searchForm").submit(function(event) { 

    /* stop form from submitting normally */ 
    event.preventDefault(); 

    /* get some values from elements on the page: */ 
    var $form = $(this), 
    term = $form.find('input[name="s"]').val(), 
    url = $form.attr('action'); 

    /* Send the data using post */ 
    var posting = $.post(url, { s: term }); 

    /* Put the results in a div */ 
    posting.done(function(data) { 
    var content = $(data).find('#content'); 
    contentstring = JSON.stringify(content); 
    $("#result").empty().append(contentstring); 

    }); 

    }); 

    </script> 
</body> 
</html> 

而这里的“formprocessing.php”的代码:

<!DOCTYPE html> 
<html lang="nl"> 

<head> 
    <meta charset="UTF-8" /> 
    <title>Contact</title> 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script> 
</head> 

<body> 

    <script> 

    <?php 
    echo "alert('Hello!');"; 
    $invoer = $_POST['s']; 
    echo (json_encode(array("content" => $invoer))); 
    ?> 

    </script> 

</body> 
</html> 

任何帮助非常赞赏。

+0

是的,你是对的......我应该只是在那里使用Javascript(在PHP开始标签之前)。正如我所说,我几乎是一个初学者。 – Holland

+0

使用firebug并查看控制台是否正确执行了ajax。 –

+0

@ Sandeep.sarkar:好主意。是的,'formprocessing.php'中的脚本似乎是正确的。也就是说,如果我在输入字段中输入“xxx”,则生成的脚本为: 。 这似乎是正确的(我已删除警报)。另一方面,内容似乎没有回到form.php,即内容没有被附加到div元素。我将进一步用Firebug进行试验。 – Holland

回答

0

你formprocessing.php文件只需要:

<?php 
    $invoer = isset($_POST['s']) ? $_POST['s'] : 'not set'; 
    echo '$invoer is:'.$invoer; 
?> 
+0

谢谢,我试过但不会改变结果。 – Holland

0

当试图此,#result我被填充了以下数据:

{"length":0,"prevObject":{"0":{},"1":{},"2":{},"3":{},"4":{},"5":{},"6":{},"7":{},"8":{},"length":9},"selector":"#content"}

也许不完全是你要找的内容对于。除此之外,事件被触发,但你犯了一些致命的错误:

  1. 有元素#content什么formprocessing.php创建。
  2. alert()将永远不会执行ajax。使用ajax,你只是静态加载数据。脚本元素在那里,但它不应该被实际执行
  3. 你可能只是想从formprocessing.php发出真正的json。要做到这一点,摆脱所有的HTML,请确保你发出application/json内容类型和只有返回json。
+0

请问您是否可以详细说明您的观点3.摆脱HTML会让我忘记这一点: <?php $ invoer = $ _POST ['s'];回声(json_encode(数组(“内容”=> $ invoer))); ?> 哪个jQuery不接受。 此外,我不知道如何“发出应用程序/ json内容类型,只返回json”。 – Holland