-1

I have a form containing a textarea for inputing text into. The form also contains a submit button. After pressing the submit button it posts the text within the textarea into my php document. Within my php document the text is added to a database. Once it has been added to the database I would like it to echo back a response telling the user that it has added the text to the database successfully. However, if i make it echo that response back to the home page, there is nowhere declared for it to display the echoed message. Has anyone got an idea of what i should be doing in order to get this working? Many Thanks.

Normally i wouldn't use a post straight from the form and i would use ajax and then display the data within a paragraph or something on it's return, however since the form is doing the post it's self i am not sure where to then declare where the response should show up.

The bellow displays my html form code and shows it's action to post to a php file.

           <div id="userban2"><form id="bannable" action="/onlineusers.php" method="post"><p> Type username to ban bellow:</p>
     <textarea name="banned" id="banned" maxlength="255"></textarea><br/>
     <input type="submit" value="Send" class="extrabuttons" onclick="return false; preventDefault();">
     <div id="cancelban" class="extrabuttons"><p> cancel</p></div>
     </form>

However when in my php file i write ....

    echo "the information has been added to the database successfully";

It might send the echo back however it isn't declared to display anywhere how can i change this to make it display the response within my form?

As requested return from my php

if(isset($_POST["banned"])){
           $ban_name = $_POST["banned"]; 
         bannedd($ban_name);
          }
          function bannedd($ban_name) {
         $query1 = mysql_query("INSERT INTO banned_users     (username,firstname,lastname,email,password,ip_address,sign_up_date,last_logged_in,about,sta    rr,userpref)     VALUES('$usernameb','$fnameb','$lnameb','$emailb','$passwordb','$ip_addressb','$sign_up_date    b','$last_logged_inb','$aboutb','$starrb','$userprefb')") or die("Could not insert your     informaion");
          echo "This user has successfully been banned"; 
        }

The form posts what is written in the form due to it having the action and method of post to my php. However should i then have any return i am not sure how i declare where the returned information should then show (The echoed message).

Nate
  • 101
  • 1
  • 8
  • are you using ajax or not? –  Nov 18 '13 at 20:04
  • Please, make it clearer how your are posting your data (is this "preventDefault()" function doing the job?) and provide the response script you wrote so far ("onlineusers.php"). – Saul Berardo Nov 18 '13 at 20:05
  • Ajax seems like the best bet here. It is made for returning messages after php interaction. I don't understand why you are not using it. – zsaat14 Nov 18 '13 at 20:06
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Nov 18 '13 at 20:18

3 Answers3

2

If I understand you correctly, your form is in some index.php file and sends the data to other file - onlineusers.php, and you want to display the message in the original page?

If this is the case, the most simple way I can think of is redirect back to the original page with a URL parameter, instead of echoing.

Do this at the end of onlineusers.php:

<?php
    // insert text into DB ...
    header("Location: index.php?result=ok");
?>

This redirects the browser back to the original page with the form. There you check if the status variable is set:

<html>
<head></head>
<body>
    <?php if(isset($_GET["result"]) && $_GET["result"]=="ok") { ?>
        <p>The information has been added to the database successfully</p>
    <?php } ?>
    <form> ... </form>
</body>
</html>

As you can probably see, you could set other results, such as "error" this way.

If you don't like the extra string in your URL, then create a cookie after processing the form in onlineusers.php and back at the original page, check if such cookie has been set. If you need more detail on that, let me know. And if you're asking something completely different, well, never mind :)

oli.G
  • 1,300
  • 2
  • 18
  • 24
  • 1
    I was thinking the same thing but with session approch :) – Stv Nov 18 '13 at 20:24
  • Yeah it's probably a cleaner, more "pro" way, and definitely a must for a bigger web application (you don't want to pollute half of your URLs) But if it's a one case scenario (i.e. a contact form on a promotional business website), I wouldn't bother :) – oli.G Nov 18 '13 at 20:44
  • Then again, in such case I wouldn't use any external PHP file and just process the form data the top of the same page before any HTML output. – oli.G Nov 18 '13 at 20:45
  • 1
    Yeah... that's what I was thinking. That's why I prefer the conditional inclusion of the needed script and then assigning a success/fail variable. The only issue I've encountered with the use of " header("Location: ABC);" is that sometimes you'll get the "Headers have been sent error message". – CharlieM Nov 18 '13 at 22:18
0

Your form is being submitted to /onlineusers.php
This is where you would want to add your echo statement.

rogerlsmith
  • 6,670
  • 2
  • 20
  • 25
0

If you require the info on the same page you technically return to the same page with the form action being $_SERVER['PHP_SELF'].

<form id="bannable" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

Then you can put in a conditional statement prior to the load of your document, and include the PHP script.

<?php 
$testVar = false; 
$msg = '';

if($_POST) {
    include '/onlineusers.php';
    //... do something e.g post to database and return true.
} 
if($testVar) {
    $msg = 'Successful writing to DB!';
}    ?>

<html>
<body>
<?php echo $msg; ?>
</body>
</html>

This will check to see if you have any post data, if you do, then it includes the script you specify. Maybe set $testVar to true if the writing to DB is successful, and then return $msg in your HTML.

CharlieM
  • 658
  • 6
  • 9