1

I am struggling very hard to get this to work and I don't know what I'm doing wrong. I have a register page that I want to take the data inserted into the form and INSERT it to the database with jQuery and AJAX. I'm not very experienced with AJAX AND jQuery so be gentle! :P I will show you the files that I have...this is a msg.php page when i have submit data sometimes post submit in database` mostly not so i want to know that why it s happening i am new in this field

 <?
    php $id=$_GET['id'];
    $id1=$_SESSION['id'];
?>
<form method="post" class="msgfrm" id="msgfrm">
<input type="hidden" value="<?php echo $_GET['id']; ?>" name="rcvrid" id="rcvrid">
<input type="hidden" name="senderid" id="senderid" value="<?php echo $id1;?>" > 
<div class="msgdiv" id="chatbox"></div>
<div class="textdiv">
   <input type="text" name="msg" id="msg" class="textmsg">
   <input type="submit"  value="Send" onClick="sendChat()">
</div>
</form>


function sendChat()
{       
   $.ajax({
           type: "POST",
           url: "msg_save.php",
           data: {  
                    senderid:$('#senderid').val(),
                rcvrid:$('#rcvrid').val(),
                msg: $('#msg').val(),
            },
           dataType: "json",
           success: function(data){
           },
        });
}

msg_save.php file

<?php
    require_once('include/util.php');
    $rcvrid=$_POST['rcvrid'];
    $senderid=$_POST['senderid'];
    $msg=$_POST['msg'];
    $sql="insert into message(rcvrid,senderid,msg) values($rcvrid,$senderid,'$msg')";
    mysql_query($sql);
?>
5
  • $id=$_GET['id']; $id1=$_SESSION['id']; why these two lines on registration page. does it has register.php?id=1 Commented Jan 18, 2014 at 5:45
  • There are a lot of places this could go wrong. We need more details on what is happening when you try to submit. Commented Jan 18, 2014 at 5:46
  • 1
    try die(mysql_error()) at the end of php code, and tell us what does it echo in your console . Commented Jan 18, 2014 at 5:50
  • put your db schema may be you messed up something in syntax. and add die statements Commented Jan 18, 2014 at 5:50
  • @susheel $id=$_GET['id']; fetch id who has send msg and $id1=$_SESSION['id']; who is sending msg Commented Jan 18, 2014 at 5:50

4 Answers 4

2
       $.ajax({
       type: "POST",
       url: "msg_save.php",
       data: " senderid="+$('#senderid').val()+"rcvrid="+$('#rcvrid').val()+"msg="+$('#msg').val(),
       dataType: "json",
       success: function(data){
       },
    });

please try this code and send data ,and use post method in php to get data,it will work

Sign up to request clarification or add additional context in comments.

1 Comment

$(this).serialize(); should be fine
1

if you are trying chat application check this, it is old but just for idea:

http://www.codeproject.com/Articles/649771/Chat-Application-in-PHP

use mysqli_query instead of mysql_query recommended

<?php
$id=$_GET['id'];
//$id1=$_SESSION['id']; COMMENTED THIS AS I AM NOT IN SESSION. HARDCODED IT IN THE FORM AS VALUE 5
?>
<html>
    <head>
        <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
    </head>
    <body>
        <form method="post" class="msgfrm" id="msgfrm">
            <input type="hidden" value="<?php echo $_GET['id']; ?>" name="rcvrid" id="rcvrid">
            <input type="hidden" name="senderid" id="senderid" value="5" > 
            <div class="msgdiv" id="chatbox"></div>
            <div class="textdiv">
                <input type="text" name="msg" id="msg" class="textmsg">
                <input type="submit"  value="Send" >
            </div>
        </form>
        <script>
            $("#msgfrm").on("submit", function(event) {
                event.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "msg_save.php",
                    data: $(this).serialize(),
                    success: function(data) {
                        $("#chatbox").append(data+"<br/>");//instead this line here you can call some function to read database values and display
                    },
                });
            });
        </script>
    </body>
</html>

msg_save.php

<?php

//require_once('include/util.php');
$rcvrid = $_POST['rcvrid'];
$senderid = $_POST['senderid'];
$msg = $_POST['msg'];
echo $rcvrid.$senderid.$msg;
$con = mysqli_connect("localhost", "root", "", "dummy");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "insert into message(rcvrid,senderid,msg) values($rcvrid,$senderid,'$msg')";
mysqli_query($con,$sql);
mysqli_close($con);
echo "successful"
?>

Comments

0

check that whether you have inserted jquery file or not.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

Then include your function sendChat() inside <script> tags.

Comments

0

On submit button

<button type="submit" id="button">SAVE</button> 
<script>
        $(document).ready(function(){
            $("#button").click(function(){
                var firstname=$("#firstname").val();
                var lastname=$("#lastname").val();
                var email=$("#email").val();

                $.ajax({
                    url:'dbConfigAndInsertionQuery.php',
                    method:'POST',
                    data:{
                        firstname:firstname,
                        lastname:lastname,
                        email:email
                    },
                   success:function(data){
                       alert(data);
                   }
                });


            });
        });
    </script>

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.