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; 
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度