Jump to content

[SOLVED] Shoutbox


Kairu

Recommended Posts

Alright.... Now that I have, for the most part, finished my dynamic image project. I have moved on to shoutboxes. I hope everyone knows what they are.... If you don't, I can probably find an example of one.

What I want to do is have it all in one file. None of the usual multiple file stuff. (I want to be able to call it through htaccess under a different name.)

What I need to know first is how to make the php file display different things given different circumstances. Say, initially I display what appears to be pure HTML, a user input field where a username and a message is input. Then submit is hit and the page refreshes, and shows a confirmation, hiding the input field and showing a previously hidden confirmation field or something. When their is an error it displays the error on the page itself without moving on.

Is this even possible with PHP? And I am aware that it would be much easier with multiple files, but that is currently not an option. (Plus it makes it a lot easier to transport and sync)
Link to comment
https://forums.phpfreaks.com/topic/32242-solved-shoutbox/
Share on other sites

Its easy enought to do with php. You would simply setup a switch. eg;

[code]
<?php
  switch ($_GET['shout']) {
    case 'edit':
      edit();
      break;
    case 'del':
      del();
      break;
    default:
      view();
  }
?>
[/code]

Then just put all your code for editing within an edit() function, all for viewing with view() etc etc....

Then if the page is called with a url such as http://yoursite.com/index.php?shout=edit the shoutbox will switch into edit mode.
Link to comment
https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149637
Share on other sites

Alright.... Got the switch working no problem, but a form is required to input the data.... I was thinking something like this, but I'm not sure at all how to get it to work.

[code]<?php
  switch ($_GET['id']) {
    case 'edit':
      edit();
      break;
    case 'view':
      view();
      break;
    default:
      error();
  }

function edit()
{
echo'<html>
<body>
<form method="POST">
<p>Username: <input type="text" name="username" size="20"></p>
<p>Message:&nbsp;&nbsp; <input type="text" name="Message" size="20"></p>
<p><input type="submit" value="Submit" name="Submit"><input type="reset" value="Reset"

name="Reset"></p>
</form>
</body>
</html>';
}

function view()
{
echo $_POST['Username'] . ' said: ';
echo $_POST['Message'];
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149652
Share on other sites

Oh! Der. Forgot that part didnt I.

I want to be able to submit the form, and have it refresh the page (well, I want it to go to the view function, so I want it to go to New.php?id=view ) with the data in the form available. I'm not quite sure how to do this, having never used a form in HTML or anything before. (I know, odd. But I've never had a use for one in the years I have been using it. )
Link to comment
https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149655
Share on other sites

I thought of that....

Here is the exact code I have. If I change it to $_POST['Message'] it works, but not $_POST['Username']...

[code]<?php
  switch ($_GET['id']) {
    case 'edit':
      edit();
      break;
    case 'view':
      view();
      break;
    default:
      error();
  }

function edit()
{
echo'<html>
<body>
<form action="thedarkrealm.no-ip.org/New.php?id=view" method="POST">
<p>Username: <input type="text" name="Username" size="20"></p>
<p>Message:&nbsp;&nbsp; <input type="text" name="Message" size="20"></p>
<p><input type="submit" value="Submit" name="Submit"><input type="reset" value="Reset" name="Reset"></p>
</form>
</body>
</html>';
}

function view()
{
echo $_POST['Username'] . ' said: ';
echo $_POST['Message'];
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149680
Share on other sites

It just wont work..... I get a blank screen. If I comment out the query, it works fine, but it does not do what I want, and I just cant figure out what I'm doing wrong.

[code]<?php
  switch ($_GET['id']) {
    case 'edit':
      edit();
      break;
    case 'view':
      view();
      break;
    default:
      error();
  }

function edit()
{
echo'<html>
<body>
<form action="New.php?id=view" method="POST">
<p>Username: <input type="text" name="Username" size="20"></p>
<p>Message:&nbsp;&nbsp; <input type="text" name="Message" size="20"></p>
<p><input type="submit" value="Submit" name="Submit"><input type="reset" value="Reset" name="Reset"></p>
</form>
</body>
</html>';
}

function view()
{
mysql_connect('localhost:3306', 'U', 'P');

mysql_select_db('gaia_image');

mysql_query("INSERT INTO box (Timestamp,Username,Message) VALUES (NOW(), $_POST['Username'], $_POST['Message'])");
}
?>[/code]

Also, I have manually inserted a row into the table using MyPHPAdmin, but I cant get the data from the cells correctly. Why will this not work?

[code]<?php
mysql_connect('localhost:3306', 'U', 'P');

mysql_select_db('gaia_image');

$result = mysql_query('SELECT * FROM box ORDER BY timestamp DESC LIMIT 1');

$row = mysql_fetch_array($result);

echo $row[0];
?>[/code]

It just comes up blank.
Link to comment
https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149735
Share on other sites

Alright, scratch all that. Not sure how fixed it, but it is fixed.

But I have a question on how to access the data that comes back.

The way I have it set up now (Since their is going to be more then one shoutbox using this table) is with ID's to separate the messages into their individual shoutboxes, and timestamps to order them.

This is the code I'm trying to use to access the data that is output.

[code]mysql_connect('localhost:3306', 'Gaia', 'Kairu');

mysql_select_db('gaia_image');

$result = mysql_query('SELECT *
FROM `box`
WHERE `id` = 1
ORDER BY `box`.`Timestamp` DESC');

$row = mysql_fetch_array($result);
echo $row[2];[/code]

In this case, it outputs "Test Text". Now my question is, how do I access the second row of data? Or the third?

I know there are three rows in the table with the ID of 1....
Link to comment
https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149854
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.