jonjon
jonjon

Reputation: 121

Passing html form value through json/ajax

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);
?>

Upvotes: 0

Views: 1312

Answers (2)

amit_183
amit_183

Reputation: 981

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;
    });

Upvotes: 1

intelis
intelis

Reputation: 8058

I don't know if thats gonna work, but i think that your data attribute should be an object.

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

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

    });
    return false;
});

Upvotes: 0

Related Questions