0

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);
?>
1
  • Small checking bits and pieces: What is the result of console.log(data) in the success block? And what is the result of a var_dump($_POST) in the script? Commented Apr 24, 2014 at 1:43

2 Answers 2

1

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;
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That definitely worked! I'm not sure why we had to use the "+" before the name but I'll look at the documentation and see if I can find out more about it. Thanks!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.