Nexy Posted June 25, 2008 Share Posted June 25, 2008 I started this in the AJAX forums because I had trouble with the ajax part of my code, now that's resolved, the user input won't insert to my db. The way I have it set up is: 1. User presses submit 2. Ajax calls process.php to insert what the user put into the textbox. 3. Process.php redirects to ajax.php to display the data. The whole set up works fine, but it doesn't insert the input. Here's the code: Form: <?php echo "<form method='post' id='form' name='form' onsubmit='return false;'> <fieldset id='shout'> <label for='shoutext'>Message:</label> <input type='text' id='shoutext' name='shoutext' tabindex='6' style='width: 360px' /> <input type='submit' id='shoutsub' name='shoutsub' value='Shout!' onclick='sendRequest(\"shout\")' tabindex='7' /> </fieldset> </form>"; ?> Process.php <?php include("includes/Connect.php"); include("includes/functions.php"); session_start(); if($_GET['mode'] == "shout") { if(isset($_POST['text'])) { $shomes = mysql_real_escape_string(addslashes($_POST['shoutext'])); $sshuser = mysql_real_escape_string(addslashes($_SESSION['username'])); $cshuser = mysql_real_escape_string(addslashes($_COOKIE['user'])); $shtime = mysql_real_escape_string(addslashes(date('h:i a'))); $shdate = mysql_real_escape_string(addslashes(date('m-j-Y'))); if($_SESSION['username']) { mysql_query("INSERT INTO shoutbox(user, time, date, message) VALUES('$sshuser', '$shtime', '$shdate', '$shomes')"); mysql_query("UPDATE users SET currency = currency + 1 WHERE username = '$sshuser'"); } else if($_COOKIE['user']) { mysql_query("INSERT INTO shoutbox(user, time, date, message) VALUES('$cshuser', '$shtime', '$shdate', '$shomes')"); mysql_query("UPDATE users SET currency = currency + 1 WHERE username = '$cshuser'"); } else { header("location: ajax.php?display=shout&err=1"); } header("location: ajax.php?display=shout"); } else { header("location: ajax.php?display=shout&err=2"); } } else { header("location: ajax.php?display=shout&err=3"); } ?> Ajax.php <?php include("includes/Connect.php"); include("includes/functions.php"); session_start(); if($_GET['display'] == "shout") { $shsql = "SELECT * FROM shoutbox WHERE date = '$shdate' ORDER BY id DESC"; $shres = mysql_query($shsql) OR die(mysql_error()); while($shout = mysql_fetch_array($shres)) { echo "<div style='text-align: left; font-size: .7em'>"; echo "[" . $shout['time'] . '] '; echo "<a href='index.php?page=Profile&user=".$shout['user']."'>" . $shout['user'] . '</a>: '; echo "<span style='color: #ADD8E6'>" . $shout['message'] . '</span><br />'; echo "</div>"; } if(mysql_num_rows($shres) == 0) { echo "<p style='color: white; margin-top: 2.3em'>Shoutbox resets everyday, type a message to start the day off.</p>"; } } ?> Sorry it's a bit long. When you click submit, it says "Shoutbox resets everyday, type a message to start the day off." until you refresh. You will see "[time] User: ". There is no message. When I look into the database, it's not even inserted, which confuses me on how it's even echoing the user and the time. I also echoed the $sql, and the "$_POST['shoutext']" message was blank. I had OR die(mysql_error()); but no errors came out, so I took it out for the time being. Any help would be appreciated. Thank You! Quote Link to comment https://forums.phpfreaks.com/topic/111774-solved-input-wont-insert/ Share on other sites More sharing options...
kenrbnsn Posted June 25, 2008 Share Posted June 25, 2008 You don't want to use addslashes & mysql_real_escape_string. You should be using stripslashes & mysql_real_escape_string. Ken Quote Link to comment https://forums.phpfreaks.com/topic/111774-solved-input-wont-insert/#findComment-573856 Share on other sites More sharing options...
.josh Posted June 25, 2008 Share Posted June 25, 2008 should if(isset($_POST['text'])) be if(isset($_POST['shoutext'])) ? Quote Link to comment https://forums.phpfreaks.com/topic/111774-solved-input-wont-insert/#findComment-573857 Share on other sites More sharing options...
Nexy Posted June 25, 2008 Author Share Posted June 25, 2008 No, $_POST['text'] is the javascript checking to see if the input box wasn't empty: I'll post the js file here too I guess: function sendRequest(mode) { if(mode == "shout") new Ajax.Request("process.php?mode=shout", { method: 'post', postBody: 'text='+ $F('shoutsub'), onComplete: showResponse }); } function showResponse(req) { $('shoutbox').innerHTML = req.responseText; } Hope this helps a bit. Quote Link to comment https://forums.phpfreaks.com/topic/111774-solved-input-wont-insert/#findComment-573862 Share on other sites More sharing options...
Nexy Posted June 25, 2008 Author Share Posted June 25, 2008 Actually, I figured it out, "$shomes = mysql_real_escape_string(addslashes($_POST['shoutext']));" should have been $_POST['text'] Thank You! Quote Link to comment https://forums.phpfreaks.com/topic/111774-solved-input-wont-insert/#findComment-573878 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.