php - jQuery AJAX method not working

27

I'm going to create a page that user can input something in textarea (index.php). When user click on Tweet, it will get all text that user typed to page tweet.php (I used jQuery Ajax method). After that, it will redirect to page show.php

Here is my code

index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Tweet</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
      function save_tweet(text_tweet){
      $.ajax
      ({
        type: "POST",
        url: "tweet.php",
        data: "text="+text_tweet,
        success: function(msg)
        {
          if(msg==1){
            window.location.href="show.php";
          }else{
            alert("Error");
          }
        }               
      });
    }

    $("#tweet").bind('click', function(){
      var text_tweet = $("#text_tweet").val();
      if(text_tweet==""){
        $("#show").html("Blank text");
        return;
      }else{
        $("#show").html("");
        save_tweet(text_tweet);
      }
    });
  });
</script>
</head>
<body>
<center>
<textarea name="text_tweet" cols="61" rows="5" id="text_tweet"></textarea>
<br/>
<div id="show"></div>
<br/>
<a href="#" id="tweet">Tweet</a>
</center>
</body>
</html>

tweet.php

<?php
session_start();
if(isset($_POST['text'])){
    $_SESSION['text_tweet'] = $_POST['text'];
}
?>

show.php

<?php
session_start();
echo $_SESSION['text_tweet'];
?>

The problem is when i input some text in textarea and click Tweet, it will alert Error. Can anyone know what is the problem?

Thank in advance.

76

Answer

Solution:

Why are you checking if msg is 1 ? Are you returning 1 in your response body ? Looking at the JQuery docs it the success function callback is described as:

Type: Function( PlainObject data, String textStatus, jqXHR jqXHR )

You could log the value of msg and see what is actually contains.

919

Answer

Solution:

Try changing your code to:

$.ajax
      ({
        type: "POST",
        url: "tweet.php",
        data: {text: text_tweet},
        success: function(msg)
        {
          if(msg==1 || msg=="1"){
            window.location.href="show.php";
          }else{
            alert("Error");
          }
        }               
      });

tweet.php

<?php
session_start();
if(isset($_POST['text'])){
    $_SESSION['text_tweet'] = $_POST['text'];
    echo 1;
}
?>
673

Answer

Solution:

try

if(msg.length){
            window.location.href="show.php";
          }else{
            alert("Error");
          }
243

Answer

Solution:

Add the dataType in your ajax:

dataType: 'text',

Add above line in your ajax code. And change your below line:

var text_tweet = $("#text_tweet").val();

with the:

var text_tweet = $("#text_tweet").text();

And in your tweet.php file add this line:

echo "1";

People are also looking for solutions to the problem: Trouble using simple SOAP call in ColdFusion, but works in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.