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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?