Jump to content

why? :S


Toy

Recommended Posts

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]')");
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

 

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.