dsn5510 2013-08-20 01:49
浏览 29

PHP post方法显然不起作用

LATEST EDIT: 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.

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.

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.

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”.

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.

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).

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!

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

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

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

EDIT: 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.

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).

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>

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>

Any help much appreciated.

  • 写回答

2条回答 默认 最新

  • douduxia1551 2013-08-20 02:12
    关注

    Your formprocessing.php file only needs:

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

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码