action="update.php" make new page :(

martynball

New Member
Messages
60
Reaction score
0
Points
0
Question 1:
Sorry about the vast amount of threads I am making, but don't want to confuse people by posting new questions in the same threads.

Here, I have a form, and action script. It updates my database with the values in the form. The only problem with it is that when I click submit, the script is loaded up and has the echo written on the screen.

I do not want this, I want it to write something like "Updated Successfully" under the submit button, or at least load a different page.

PHP:
<form name="form1" method="post" action="scripts/update.php" onsubmit="return validate_form(this)">
        <p class="g2">Name:</p>
        <input type="text" class="contact-feild" name="name" size="20" />
        <br />
        <p class="g2">Email:</p>
        <input type="text" class="contact-feild" name="email" size="20" />
        <br />
        <p class="g2">Message:</p>
        <textarea name="message" class="contact-feild" style="width:300px; height:150px; overflow:scroll; overflow-x:hidden;">
  </textarea>
        <br />
        <input type="submit" value="Submit" class="button" name="submit"/>
      </form>

update.php
PHP:
<?php
// Connect to database
$con = mysql_connect("localhost","martynba_martynb","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  // Select table
mysql_select_db("martynba_comments", $con);

// Make varibles
date_default_timezone_set('GMT');
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$message = mysql_real_escape_string($_POST['message']);
$date = date("F j, Y, g:i a");

// Insert data
$query="INSERT INTO commentTable ( NAME, EMAIL, MESSAGE, DATE)
VALUES ('".$name."','".$email."','".$message."','".$date."')";
$result = mysql_query ($query);

    echo "<p class=\"g2\">Thanks for your comment $name!</p>";

?>

-------------------------------------------------

Question 2:
How can I make this php code display the results in a certain order, e.g. smallest "id" number first.
PHP:
  <?php
// Connect to the database server
mysql_connect('localhost', 'martynba_martynb', 'password') or die('Error connecting to the database: ' . mysql_error());
// Select the database
mysql_select_db('martynba_comments') or die('Error selecting database: ' . mysql_error());

//Get data...
$result = mysql_query("select * from commentTable");
while ($row = mysql_fetch_array($result))
{ 
$id=$row["ID"];
$name=$row["NAME"];
$message=$row["MESSAGE"];
$email=$row["EMAIL"];
$today= $row["DATE"];

//Display results
	echo "<table class=\"comment-table\">";
	echo "<tr>";
	echo "<td class=\"comment-name\">Posted by: $name<div class=\"comments-date\">$today</div></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"comment-message\">$message";
	echo "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"comment-id\">ID: $id";
	echo "<form method=\"post\" action=\"scripts/delete.php\" class=\"del-form\"><input name=\"id\" id=\"id\" type=\"hidden\" value=\"$id\"><input type=\"image\" src=\"../images/del.png\"></form></td>"; 
	echo "</tr>";
	echo "</table>";
	echo "<br />";
}
?>
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Sorry about the vast amount of threads I am making, but don't want to confuse people by posting new questions in the same threads.
You're doing the right thing. New issues deserve new threads for exactly the reason you say: it prevents confusion. You can always include links to other relevant threads.

You posted your DB username and password again; better edit your post.

I do not want this, I want it to write something like "Updated Successfully" under the submit button, or at least load a different page.

HTML:
<form name="form1" method="post" action="scripts/update.php" onsubmit="return validate_form(this)">
        <p class="g2">Name:</p>
        <input type="text" class="contact-feild" name="name" size="20" />
        <br />
With the <p> elements, the <br/> are unnecessary. <br/> have no semantic value; avoid them if possible (and it usually is possible).

I do not want this, I want it to write something like "Updated Successfully" under the submit button, or at least load a different page.
...
update.php
PHP:
// Insert data
$query="INSERT INTO commentTable ( NAME, EMAIL, MESSAGE, DATE)
VALUES ('".$name."','".$email."','".$message."','".$date."')";
$result = mysql_query ($query);
Test the result; make sure it's not FALSE and at least one row was affected. If so, query was successful. If not, query failed. You can then either print a message or use header to redirect to another page. Note that header only works if NOTHING has yet been printed, not even spaces or the UTF-8 byte order mark, since no HTTP headers can be sent once you start sending the content.

In some other file:
PHP:
// For illustration only. Not production-ready code.
function uriScheme() {
    if ($_SERVER['SERVER_PORT'] == 443) {
        return 'https://';
    } else {
        return "http://";
    }
}

function uriPort() {
    switch ($_SERVER['SERVER_PORT']) {
    case 443:
    case 80:
        return '';
    default:
        return ':' . $_SERVER['SERVER_PORT'];
    }
}

function scriptBase() {
    static $base = null;
    if (is_null($base)) {
        $base = uriScheme() . $_SERVER['HTTP_HOST'] . uriPort() . dirname($_SERVER['REQUEST_URI']);
    }
    return $base;
}

In "update.php":
PHP:
if ($result !== FALSE && mysql_num_rows($result) > 0) {
    header("Location: " . scriptBase() . '/result.php?success' );
    // OR
    echo "Update successful.";
} else {
    header("Location: " . scriptBase() . '/result.php?fail&errno=' . mysql_errno() );
    // OR 
    echo "Update failed.";
    // Now give a more specific and helpful message.
    switch (mysql_errno()) {
        ...
    }
}

Question 2:
How can I make this php code display the results in a certain order, e.g. smallest "id" number first.
PHP:
  <?php
//Get data...
$result = mysql_query("select * from commentTable");

Use the "ORDER BY" clause:
Code:
SELECT * FROM commentTable ORDER BY id; -- smallest ID to largest
SELECT * FROM commentTable ORDER BY name DESC; -- largest to smallest
SELECT * FROM commentTable ORDER BY date, name; -- sort by date first, then name

You should really find some good books or tutorials to read right now; they'll answer the questions you've been asking and more.
 

martynball

New Member
Messages
60
Reaction score
0
Points
0
One more quick question, when I enter txt longer than the table, it stretches the table... i want it go on a new line, like it was doing. :S

http://martynleeball.x10hosting.com/contactus.php

Click "Show Comments" at bottom of page.

PHP:
<?php
// Connect to the database server
mysql_connect('localhost', 'martynba_martynb', 'password') or die('Error connecting to the database: ' . mysql_error());
// Select the database
mysql_select_db('martynba_comments') or die('Error selecting database: ' . mysql_error());

//Get data...
$result = mysql_query("select * from commentTable ORDER BY date;");
while ($row = mysql_fetch_array($result))
{ 
$id=$row["ID"];
$name=$row["NAME"];
$message=$row["MESSAGE"];
$email=$row["EMAIL"];
$today= $row["DATE"];

//Display results
	echo "<table class=\"comment-table\">";
	echo "<tr>";
	echo "<td class=\"comment-name\"><div class=\"name-c\">Posted by: $name</div>
	<div class=\"comments-date\">$today</div></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"comment-message\">$message</div>";
	echo "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"comment-id\"><div class=\"name-c\">ID: $id</div>";
	echo "</td>"; 
	echo "</tr>";
	echo "</table>";
	echo "<br />";
}
?>
 
Last edited:

spadija

New Member
Messages
48
Reaction score
1
Points
0
The css property "word-wrap: break-word" is supposed to force a line break. I say supposed to because it isn't implemented in all browsers yet. Check out this page for more information: http://www.css3.com/css-word-wrap/ The comments also contain some browser-specific css to get it working in Firefox and IE.

You can also use the "max-width" property to keep the boxes from stretching, but then the text will overflow. You can use the "overflow" property to hide overflow or add scroll bars when the text is too long.

There really isn't an elegant solution to this yet. One way to handle it is to use PHP insert a soft hyphen into long words, but there's no way to get the rendered width of text from PHP, so you run into problems with different fonts and text sizes on different browsers.

Hope that answers your question.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Another issue with the page you link to (http://martynleeball.x10hosting.com/contactus.php) is that the bottom bar covers up "Show comments" in FF 3.5 and Safari 4.0 (and probably others). The simplest solution is to put a bottom padding of 1em on .main_text. With this option, the scrollbar will extend down next to the bottom bar. Another option is to use the old IE 6 fixed positioning emulation, which places everything but the bottom bar in an absolutely position element. With this option, the scrollbar will stop just short of the bottom bar. Something like:
HTML:
<!DOCTYPE ...>
<html>
  <head>...
    <style type="text/css">
        body { overflow: hidden; }
        #Body {
            position: absolute;
            top: 0px;
            left: 0px;
            bottom: 1em;
            right: 0px;
            overflow: auto;
        }
        .bottom-bar {
            position: absolute;
            bottom: 0px;
            left: 0px;
            width: 100%;
            height: 1em;
            z-index: 1;
        }
    </style>
  </head>
  <body>
    <div id="Body">
        ....
    </div>
    <div class="bottom-bar"></div>
  </body>
</html>

Unless you really don't like the scrollbar next to the bottom bar, I'd go with padding over restructuring the HTML.

Forgot to mention earlier: the class "contact-feild" in the contact form is misspelled; it should be "contact-field".
 
Top