Reputation: 215
I'm really stuck on what to do. My problem is that a PHP variable (or javascript/jQuery) is not being sent to another php file through an AJAX request that I have created. Ok, I am creating a chat application. It has a message textbox and a send button. When the send button is clicked, the ajax request works and it executes the php file I want it to (and I know this because it writes a ":" to my text file, which is correct), except that I get: "undefined index: message", because the message variable is not being sent to the php file! I have found numerous questions on this topic, but none of them seemed to help me, so I was hoping you guys would be able to figure it out.
Here is my test.php file:
<!doctype html>
<html>
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#chat").load("chat.txt");
});
</script>
<script>
$(document).ready(function(){
$("#send").click(function(){
$.ajax({
type: 'post',
url: 'chat.php',
data: '' + message,
success: function(response){
$("#chat").html(response);
}
});
});
});
</script>
</head>
<body>
<?php
if (isset($_POST['send'])) {
$message = $_POST['message'];
}
?>
<style type="text/css">
#chat {
width: 194px;
background-color: #292929;
color: #493C9E;
overflow: scroll;
}
</style>
<pre id="chat">
</pre>
<form method="post">
Message: <input type="text" id="message" name="message"><br>
<input type="submit" value="Send" id="send" name="send" onclick="return false;">
</form>
</body>
</html>
I have tried everything I can think of and nothing is working! I have tried creating a message variable using JavaScript and jQuery, I have tried to insert php tags and create a php variable next to data. I've tried with and without an empty string. Some other code I tried was data: { message: '123' }
. Also when I created a jQuery/JavaScript variable I alerted the value of the message, and it was exactly what I had typed into the message textbox.
chat.php:
<?php
$file = "chat.txt";
$handle = fopen($file, "a+");
fwrite($handle, ": " . $message . "\n");
echo "\n: " . $message;
?>
I have tried including the test.php file in the chat.php file, but to no avail. I also created the message variable $message = $_POST['message'];
and the message data was meant to be sent from the test.php file. I've tried checking if the message variable was set (using isset()
). In the test.php file I also tried creating the message variable through AJAX, { message: $message },
. And NOTHING seems to be working AT ALL!!!! This is really getting to me, so if someone could please help me, I'd really appreciate it.
Thanx ALOT in advance :)
PS: If you need any other information, please just comment below, and I'll give it to you. And thanks to everyone for responding so fast in my previous questions :)
Upvotes: 1
Views: 599
Reputation: 1243
Few issues I identified:
Move your isset()
to chat.php, not inside test.php. If you have $.ajax()
, then why need to have isset()
but use it in your chat.php as part of grabbing and processing $_POST[]
.
You are missing var
and your ajax doesn't know which string to send.
Wrap up all of your jquery snippets into one tag instead of having 2 $(document).ready(function()
I always put my jQuery in the bottom of html ( right before </body>
instead of inside <head>
.
So as per my feedback, the following below would be what it should appear as per my coding approach:
test.php
<!doctype html>
<html>
<head>
<style type="text/css">
#chat {
width: 194px;
background-color: #292929;
color: #493C9E;
overflow: scroll;
}
</style>
<script src="jquery.min.js"></script>
</head>
<body>
<pre id="chat">
</pre>
<form id="chatForm" method="post">
Message: <input type="text" id="message" name="message"><br>
<input type="submit" value="Send" id="send" name="send" onclick="return false;">
</form>
<script type="text/javascript">
/* notice I moved the jQuery script to bottom right before </html> instead of inside <head> */
/* combine both of 2 jQuery into same grouping */
$(document).ready(function(){
$("#chat").load("chat.txt");
/* use form id for ajax to manipulate the data so, I added id="chatForm" in your html form for this one: */
$("#chatForm").submit(function(){ /* <-- use `submit()` so it'd handle the whole form at once */
var message = $( this ).serialize(); /* <-- this inkove ajax to know to expect string before sending to php */
$.ajax({
type: 'post',
url: 'chat.php',
data: ''+ message,
success: function(response){
$("#chat").html(response);
} // end of success
}); // end of $.ajax()
}); // end of submit
});// end of document ready
</script>
</body>
</html>
and in your chat.php
<?php
/* transfer isset() from test.php to this file and use it to wrap your fwrite function */
if (isset($_POST['message'])) {
$message = $_POST['message'];
$file = "chat.txt";
$handle = fopen($file, "a+");
fwrite($handle, ": " . $message . "\n");
echo "\n: " . $message;
}
?>
Upvotes: 1
Reputation: 818
You must take the variable with $_POST
in chat.php
Like this:
<?php
$file = "chat.txt";
$handle = fopen($file, "a+");
fwrite($handle, ": " . $_POST['message'] . "\n");
echo "\n: " . $_POST['message'];
?>
Upvotes: 0
Reputation: 2460
Add an error function to you ajax call and see what is returned.
error: function(msg){alert(msg.status + " " + msg.statusText);}
Upvotes: 0