Results 1 to 2 of 2

Thread: ajax help

  1. #1
    Join Date
    Jul 2010
    Location
    NangYang
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default ajax help

    Hi,
    I am trying an ajax technique to insert data but it doesn't work.
    here is the code
    The purpose is that insert the message of a chatbox to db

    PHP Code:
    <?php

    echo"<form  name='chat'>";
    if (!isset(
    $_COOKIE["user"])|| $_COOKIE["user"]=="")
    {
        echo 
    "Your name";
    echo 
    "<input type='text' size=20 id='name'>";
    }
    echo 
    "<textarea cols='21' rows='5' id='message' >Write here</textarea>";
    echo 
    "<input onclick='loadContent()' style=' background-color:#0F6' name='CHAT' type='submit' value='CHAT' />";
    echo 
    "</form>";
    ?>
    HTML Code:
    <script type="text/javascript" >
            function loadContent(){
                //Browsers support
            xmlhttp=new XMLHttpRequest();
            var name=document.getElementById("name").value;
            var message=document.getElementById("message").value;
            xmlhttp.open("GET","chatbox.php",true);
            xmlhttp.send(null);
            document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                }
    </script>

    PHP Code:
    <?php
    header
    ("location: index.php");
    include (
    "dbconnect.php");
    /*if(isset($_POST['CHAT']))
    {
    if (!isset($_COOKIE["user"])|| $_COOKIE["user"]=="")
    {
        if ($_POST['name']==NULL) exit();
        else 
        $username=$_POST['name'];
        }
        else 
        {
            $username=$_COOKIE["user"];
        }
        $message=$_REQUEST['message'];*/
        
    $name=$_GET["name"];
        
    $message=$_GET["message"];
        
    $date=date("d/ m h:i");
    $query="insert into chatbox(username,chat,time) values ('$username','$message','$date')";
    mysql_query($query) or die(mysql_error());

    }
    ?>

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I think your problem is your ajax request.

    Code:
    xmlhttp.open("GET","chatbox.php",true);
    should be

    Code:
    xmlhttp.open("GET","chatbox.php?name=" + name + "&message=" + message,true);
    Actually you should move the header down to the end of the process as well.

    PHP Code:
    <?php
    include ("dbconnect.php");
    /*if(isset($_POST['CHAT']))
    {
    if (!isset($_COOKIE["user"])|| $_COOKIE["user"]=="")
    {
        if ($_POST['name']==NULL) exit();
        else 
        $username=$_POST['name'];
        }
        else 
        {
            $username=$_COOKIE["user"];
        }
        $message=$_REQUEST['message'];*/
        
    $name=$_GET["name"];
        
    $message=$_GET["message"];
        
    $date=date("d/ m h:i");
    $query="insert into chatbox(username,chat,time) values ('$username','$message','$date')";
    mysql_query($query) or die(mysql_error());
    //}
    header("location: index.php");
    ?>
    Last edited by bluewalrus; 10-04-2010 at 08:41 PM. Reason: spelled problem wrong
    Corrections to my coding/thoughts welcome.

  3. The Following User Says Thank You to bluewalrus For This Useful Post:

    jangkoo (10-05-2010)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •