dongshao8566 2010-11-16 14:35
浏览 33
已采纳

php-如何使用ajax发布值?

test.html

<html>
    <!--
     Please see the full php-ajax tutorial at http://www.php-learn-it.com/tutorials/starting_with_php_and_ajax.html
     If you found this tutorial useful, i would apprciate a link back to this tutorial. 
     Visit http://www.php-learn-it.com for more php and ajax tutrials
     -->
    <title>php-learn-it.com - php ajax form submit</title>
    <head>      
        <script type="text/javascript" src="prototype.js"></script>
        <script>

            function sendRequest() {
                new Ajax.Request("test.php", 
                    { 
                    method: 'post', 
                    postBody: 'name='+ $F('name'),
                    onComplete: showResponse 
                    });
                }

            function showResponse(req){
                $('show').innerHTML= req.responseText;
            }
        </script>
    </head>

    <body>
        <form id="test" onSubmit="return false;">
            <input type="hidden"  name="name" id="name" value="value">
            <input type="hidden" name="somethingElse" value="test" value="submit" onClick="sendRequest()">
        </form>
        <a href="#" onclick="myForm.submit()">Post!</a>

        <div id="show"></div>
        <br/><br/>

    </body>

</html>


<?php
/*
 * Please see the full php-ajax tutorial at http://www.php-learn-it.com/tutorials/starting_with_php_and_ajax.html
 * If you found this tutorial useful, i would apprciate a link back to this tutorial. 
 * Visit http://www.php-learn-it.com for more php and ajax tutrials
 */

if($_POST["name"] == "")
    echo "name is empty";
else
    echo "you typed ".$_POST["name"];
?>

Where is the wrong in form thanks.

  • 写回答

2条回答 默认 最新

  • douzhizao0270 2010-11-16 14:45
    关注

    A few things are wrong here:

    1. You have an onclick on a hidden form element
    2. The Post! anchor onclick isn't submitting anything

    Suggested changes:

    <head>      
        <script type="text/javascript" src="prototype.js"></script>
        <script>
            function showResponse(req){
                $('show').innerHTML = req.responseText;
            }
        </script>
    </head>
    
    <body>
        <form id="test" name="test" action="test.php">
            <input type="hidden"  name="name" id="name" value="cats" />
            <input type="hidden" name="somethingElse" value="test" />
        </form>
        <a href="#" onclick="$('test').request({onComplete: showResponse})">Post!</a>
    
        <div id="show"></div>
    
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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