Toy Posted February 2, 2011 Share Posted February 2, 2011 I'm trying to load a PHP action with a button, I have a "if isset post..." you know what I mean, and it works great, but it wont echo the output, only insert it in the database...wtf am I doing wrong? :S example: if (isset($_POST['submit'])) { $xx = 'lol'; echo ''.$xx.', yo dawg!'; mysql_query("INSERT INTO xx (xx, xx) VALUES ('$xx', '$user[username]')"); } Quote Link to comment https://forums.phpfreaks.com/topic/226454-why-s/ Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Share Posted February 2, 2011 I'm trying to load a PHP action with a button, I have a "if isset post..." you know what I mean, and it works great, but it wont echo the output, only insert it in the database...wtf am I doing wrong? :S example: if (isset($_POST['submit'])) { $xx = 'lol'; echo ''.$xx.', yo dawg!'; mysql_query("INSERT INTO xx (xx, xx) VALUES ('$xx', '$user[username]')"); } Try this; if(isset($_POST['submit'])) { $xx = 'lol'; echo $xx . ', yo dawg!'; mysql_query("INSERT INTO `xx` (`xx`, `xx`) VALUES ('" . $xx . "', '" . $user[username] . "')") or die(mysql_error()); } James. Quote Link to comment https://forums.phpfreaks.com/topic/226454-why-s/#findComment-1168845 Share on other sites More sharing options...
The Little Guy Posted February 2, 2011 Share Posted February 2, 2011 what does the form look like? Quote Link to comment https://forums.phpfreaks.com/topic/226454-why-s/#findComment-1168934 Share on other sites More sharing options...
Psycho Posted February 2, 2011 Share Posted February 2, 2011 @james: There is no reason to "exit" a double quoted string to include variables. One of the features of using a double quoted string is that variables within the string will be interpreted. However, one problem with the original code posted is the array value. The key for the array was NOT enclosed in quotes. It will work, but is not correct. The parser will first try to find a constant named "username" before it treats it as a string. When using variables in double quotes you can use curly braces around variables so that you can use quotes around keys and to solve other problems where the variable may be misinterpreted due to the context it is used in. Here is a another method of defining that code: if (isset($_POST['submit'])) { $xx = 'lol'; echo "{$xx}, yo dawg!"; mysql_query("INSERT INTO xx (xx, xx) VALUES ('{$xx}', '{$user['username']}')"); } @Toy: This is a shot in the dark, but are you running this code from an AJAX request? If so, your javascript code needs to have a handler to receive the input from the PHP page and do something with it. If this page is being called to be loaded in the browser and that message is not being displayed, one possible reason would be if there is a redirect OR the output is placed within other HTML code such that it is not being rendered correctly. Quote Link to comment https://forums.phpfreaks.com/topic/226454-why-s/#findComment-1168951 Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Share Posted February 2, 2011 @james: There is no reason to "exit" a double quoted string to include variables. One of the features of using a double quoted string is that variables within the string will be interpreted. However, one problem with the original code posted is the array value. The key for the array was NOT enclosed in quotes. It will work, but is not correct. The parser will first try to find a constant named "username" before it treats it as a string. When using variables in double quotes you can use curly braces around variables so that you can use quotes around keys and to solve other problems where the variable may be misinterpreted due to the context it is used in. Here is a another method of defining that code: if (isset($_POST['submit'])) { $xx = 'lol'; echo "{$xx}, yo dawg!"; mysql_query("INSERT INTO xx (xx, xx) VALUES ('{$xx}', '{$user['username']}')"); } @Toy: This is a shot in the dark, but are you running this code from an AJAX request? If so, your javascript code needs to have a handler to receive the input from the PHP page and do something with it. If this page is being called to be loaded in the browser and that message is not being displayed, one possible reason would be if there is a redirect OR the output is placed within other HTML code such that it is not being rendered correctly. Right you are mjdamato, my bad . James. Quote Link to comment https://forums.phpfreaks.com/topic/226454-why-s/#findComment-1168960 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.