Rasputin
New Member
- Joined
- Feb 25, 2011
- Messages
- 85 (0.02/day)
- Location
- Cumbria, United Kingdom
System Name | My build |
---|---|
Processor | AMD Llano A8-3850 APU |
Motherboard | Gigabyte GA-A75M-UD2H |
Memory | 8GB G.Skill Ripjaws 1600MHz |
Storage | Samsung Spinpoint 1TB |
Display(s) | HANNS-G HH221 1920x1080 |
Case | Coolermaster Elite 331 |
Power Supply | 500W |
So i'm making a browser based instant messenger using javascript and PHP.
There is a form on the page which sends the message to a MySQL database using the POST method.
Upon loading the page to write messages, the user is prompted to enter a username which is then held in a js variable. Now I figured out that you can't transfer the contents of a js var to a PHP var, so instead I have been trying to have the page write the value of the username var to a hidden textbox on the form.
I can make the js write the value of "nickname" to the textbox, but when I submit the form: "Undefined index: nickname in C:\wamp\www\new\insert.php on line 11".
I don't understand this though: the column is part of the mysql table it adds the data to, it was not doing before I worked out how to add the value of the variable to the text field on the form.
This is the "Chat.php" that the form is on.
This is the insert.php which adds the data to the table.
If you need any more information, just ask.
Thank you for any help, fellow TPU members!
There is a form on the page which sends the message to a MySQL database using the POST method.
Upon loading the page to write messages, the user is prompted to enter a username which is then held in a js variable. Now I figured out that you can't transfer the contents of a js var to a PHP var, so instead I have been trying to have the page write the value of the username var to a hidden textbox on the form.
I can make the js write the value of "nickname" to the textbox, but when I submit the form: "Undefined index: nickname in C:\wamp\www\new\insert.php on line 11".
I don't understand this though: the column is part of the mysql table it adds the data to, it was not doing before I worked out how to add the value of the variable to the text field on the form.
This is the "Chat.php" that the form is on.
Code:
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="style.css" type="text/css" />
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
// ask user for name with popup prompt
function setNick(){
var nickname = prompt("Enter your chat name:", "Guest");
document.getElementById("nick").value = nickname;
return false;
}
</script>
</head>
<body onLoad="setNick();">
<div id="chat-wrap">
<div id="chat-area"></div>
<form name="send" action="insert.php" method="POST">
<input type="text" name="txtInput" value="Enter message..." size="50" />
<input type="text" id="nick" readonly="readonly" />
<input type="submit" value="Submit" name="btnSubmit" />
</form>
</div>
</body>
</html>
This is the insert.php which adds the data to the table.
Code:
<?php
$con = mysql_connect("localhost","********","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("chat1", $con);
$sql="INSERT INTO msg (name, message)VALUES('$_POST[nick]','$_POST[txtInput]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
If you need any more information, just ask.
Thank you for any help, fellow TPU members!
Last edited: