Jump to content

Recommended Posts

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! :)

Link to comment
https://forums.phpfreaks.com/topic/111774-solved-input-wont-insert/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.