duanjuelu8874 2014-04-24 01:37
浏览 54
已采纳

通过json / ajax传递html表单值

I have a basic html form and I'm trying to use jquery and json to pass the value of the form to PHP. I'm new at this, but I did a lot of research and I thought I had everything entered correct. The page no longer reloads when I hit submit (and that's the behavior I want), but I coded an javascript alert to see if the data is being passed and that alert is not working. I've been looking at this code for hours and I can't seem to find any errors. Can someone tell me why the alert is not working?

here is my main.php

<!DOCTYPE html>
<html>
<head>

    <title>leader</title>


<link rel="stylesheet" type="text/css" href="main.css">

</head>

<body>
<div class = "container">
<form action="post.php" method="post" id="add">
<input type="text" class="leader" name="name" placeholder="Leader">
<input type="submit" value="send" />
</form>


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.10.4.custom.js"></script>
<script src="globe.js"></script>


<ul id ="hours_zoned">
    <li class="nine">9</li>
    <li class="ten">10</li>
    <li class="eleven">11</li>
    <li class="twelve">12</li>
    <li class="one">1</li>
    <li class="two">2</li>
    <li class="three">3</li>
    <li class="four">4</li>
    <li class="five">5</li>
    <li class="six">6</li>
    <li class="seven">7</li>
    <li class="eight">8</li>
    <li class="nine">9</li>
</ul>
</div>  




</body>
</html>

here is my globe.js

$('#add').on('submit', function() {
        var name = $('.leader').val();

        $.ajax({
        url: 'post.php',
        dataType: 'json',
        type: 'post',
        data: name,
        success: function (data) {
            if(data.success) {
                alert('the result is ' + data.result);
                }
        }

        });
        return false;
    });

here's my post.php

<?php
header('Content-type: text/javascript');

$json = array(
    'success' => false,
    'result' => 0

);


if(isset($_POST['name'])) {
    $name = $_POST['name'];

    $json['success'] = true;
    $json['result'] = $name;
}

echo json_encode($json);
?>
  • 写回答

2条回答 默认 最新

  • dsideal2015 2014-04-24 04:00
    关注

    Make the changes in globe.js and it works:

    $('#add').on('submit', function() {
            var name = $('.leader').val();
    
            $.ajax({
            url: 'post.php',
            dataType: 'json',
            type: 'post',
            data: 'name='+name,
            success: function (data) {
                if(data.success) {
    
                    alert('the result is ' + data.result);
                    }
            }
    
            });
            return false;
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧