PHP发布方法显然不起作用 [英] PHP post method apparently not working

查看:59
本文介绍了PHP发布方法显然不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最新事实证明,在formprocessing.php中,isset($ _ POST ['submit1'])为FALSE(其中,"submit1"现在是提交按钮的名称;最初它没有名称).如下面的代码所示,我最初并未对此进行测试.

LATEST It turns out that, in formprocessing.php, isset($_POST['submit1']) is FALSE (where 'submit1' is now the name of the submit button; originally it did not have a name). As seen in the code below, I did not test for this originally.

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

It does explain a lot. But the remaining question is why isset($_POST['submit1']) is FALSE.

注意:该问题已经过编辑,以反映最新的见解.

NOTE: This question has been edited to reflect more recent insights.

作为PHP的初学者,我正在尝试将表单发布到服务器,但是它不起作用.在所有可能的情况下,我都忽略了一些简单的事情,但我只是没有看到它.

As a PHP beginner, I’m experimenting with posting forms to the server, and it does not work. In all likelyhood I’m overlooking something simple, but I just don’t see it.

我有两个文件:"form.php"和"formprocessing.php",它们都位于同一文件夹中.我已经在下面包含了完整的代码,但首先要进行一些解释.文件"form.php"包含表单本身,包括"post"方法.可以说,文件"formprocessing.php"是发布"方法的目标,即"action = formprocessing.php".

I have two files: ‘form.php’ and ‘formprocessing.php’, both located in the same folder. I have included the full code below, but first some explanation. The file ‘form.php’ contains the form itself, including the ‘post’ method. The file ‘formprocessing.php’ is the destination, so to speak, of the ‘post’ method, i.e. "action = formprocessing.php".

这个想法是表单处理应该在不加载"formprocessing.php"或重新加载"form.php"的情况下进行(因此,在form.php中为"event.preventDefault();").我知道我也可以发布到"form.php"本身,但现在我想使用两个单独的文件.

The idea is that the formprocessing should take place without 'formprocessing.php' loading or 'form.php' reloading (hence "event.preventDefault();" in form.php). I’m aware that I could also be posting to ‘form.php’ itself, but for now I want to go with two separate files.

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

I’ve taken the code of ‘form.php’ from the jQuery-site almost literally (http://api.jquery.com/jQuery.post/ , the last example). I’ve changed the file name in the action attribute to ‘formprocessing.php’, and added a necessary JSON.stringify command (see below).

"formprocessing.php"中的其余代码仅提取发布的输入字段的值(表单中只有一个字段),然后以JSON的形式将其返回给"form.php"-object(然后在form.php中进行字符串化).至少就是这个主意!

The rest of the code in ‘formprocessing.php’ simply extracts the value of the posted input field (there is only one field in the form), and gives it back to ‘form.php’ in the form of a JSON-object (which is then stringified in form.php). At least that’s the idea!

在"formprocessing.php"处生成的脚本.-在输入字段中输入"xxx"后,如下所示:

The script generated at 'formprocessing.php.' - after typing, say, "xxx" into the input field - is as follows:

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

现在在我看来,这个脚本SEEMS是正确的-但这是吗?这是我现在想确定的第一件事.

Now to my eyes, this script SEEMS correct -- but is it? That is the first thing I'd like to know for sure now.

我现在已经删除了formprocessing.php中的所有HTML标记.这也意味着生成的脚本减少为{"content":"xxx"},即仅是JSON对象.不过不会改变结果.

I've now removed all the HTML tags in formprocessing.php. Which also means that the generated script reduces to {"content":"xxx"} , i.e. only the JSON-object. Doesn't change the result though.

因为以后确实会出问题.也就是说,在"form.php"的末尾,我将从"formprocessing.php"返回的数据内容附加到div元素上.但实际上,附加到div的字符串实际上是"{"length":0,"prevObject":{"0":{},"1":{},"2":{},"3":{},"4":{},"5":{},"6":{},"7":{},"8":{},"length":9},选择器":"#content"}-即表示长度为0的对象.而不是所谓的从"formprocessing.php"返回的实际数据(正如我所说,它也等于对form字段的原始输入).

Because something does go wrong later. That is, at the end of ‘form.php’, I attach the contents of the data returned from ‘formprocessing.php’ to a div element. But in fact the string attached to the div turns out to be "{"length":0,"prevObject":{"0":{},"1":{},"2":{},"3":{},"4":{},"5":{},"6":{},"7":{},"8":{},"length":9},"selector":"#content"}" -- i.e., indicating an object of length 0. Rather than the actual data supposedly returned from 'formprocessing.php' (which as I said also equal the original input to the form field).

以下是"form.php"的代码:

Here comes the code of ‘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"的代码:

And here’s the code of ‘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>

非常感谢任何帮助.

推荐答案

尝试此操作时,对我来说 #result 充满以下数据:

When trying this, #result for me gets filled with the following data:

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

可能不是您想要的全部.除此之外,该事件被触发,但您犯了一些致命的错误:

Probably not entirely what you're looking for. Aside from that the event gets triggered, but you're making a few fatal mistakes:

  1. formprocessing.php创建的ID为#content的元素.
  2. alert()将永远不会与ajax一起执行.使用ajax,您只是静态地加载数据.脚本元素在那里,但不应实际执行
  3. 您可能只想从formprocessing.php发出真实的json.为此,请删除所有html,确保发出 application/json 内容类型,并且 only 返回json.
  1. There is element with id #content what formprocessing.php creates.
  2. alert() will never be executed with ajax. With ajax, you are just loading the data statically. The script element is there, but it's not supposed be actually executed
  3. You probably just want to emit real json from formprocessing.php. To do this, get rid of all the html, make sure you emit a application/json content type and only return json.

这篇关于PHP发布方法显然不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆