www.webdeveloper.com
Results 1 to 6 of 6

Thread: Problem passing parameters from form to function

  1. #1
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101

    Question Problem passing parameters from form to function

    Hi!
    I'm presently redoing my website, and I'm using wordpress as a CMS. I wish to re-use a chatboard I had on the old website, but I'm having problems passing the variables through. What could be the problem?
    Here's my form:

    HTML Code:
    <form action="http://www.mydomain.com/chat/post.php" method="post" id="comment-form">
    <fieldset>
    	<p style="float:left"><label for="user_name">Nom ou pseudo&nbsp;:</label><br />
    	<input name="user_name" id="user_name" type="text" size="25" maxlength="255" /></p>
    	
    
    	<p style="float:right"><label for="user_email">URL&nbsp;:</label><br />
    	<input name="user_email" id="user_email" type="text" size="25" maxlength="255" value="http://" /></p>
    
    	<p><label for="chat_body">Commentaire&nbsp;:</label><br />
    	<textarea class="center" id="chat_body" name="chat_body" cols="57" rows="3"></textarea>
    	</p>
    </fieldset>
    
    <fieldset>	
    <p class="center pad"><input type="submit" class="submit" name="Sign" value="Go !" size="25" /></p>
    </fieldset>	
    </form>
    That's my post.php file:

    PHP Code:
     // my connexion parameters to DB here

    // do a basic check, don't let user submit a blank entry.

    if ($user_name == '' || $chat_body == '' )
    {
        echo 
    "error message";
    die;

    }

    // strips off weird characters and HTML tags
    $allowable_tags "<B><I><LI><OL><P><U><UL><S>";
    htmlspecialchars($user_name);
    $chat_body strip_tags($chat_body$allowable_tags);

    // do check here

    $last_entry "select * from shoutbox order by id desc limit 1";
    $check mysql_query($last_entry);

    while (
    $row mysql_fetch_array($check))
    {
    $oldname $row["user_name"];
    $oldmsg $row["chat_body"];
    }

    // compare the values of old and new entry
    if ($oldname == $user_name && $oldmsg == $chat_body// means the name and msg entered is the same
    {
        echo 
    "Another error message here";
    die;



    // check comments :

    /**
    * Check presence of 'bad strings'.
    */
    function contains_badwords($string) {
    $bad_words = array(
                          
    // here, an array of spam words          
                         
    );
       foreach(
    $bad_words as $bad_word) {
         if(
    eregi($bad_wordstrtolower($string))) {
            return 
    true;
         }
       }
       return 
    false;

     
    if (
    contains_badwords($chat_body)) {
       
    header("HTTP/1.0 200 OK"); //Send out a content is good/complete to make it look as though things have gone well
        
    header('Refresh: 1; url=http://spam.abuse.net/'); //polite redirect
          
    header("Connection: close");
          exit(); 
    }

    if (
    contains_badwords($user_name)) {
       
    header("HTTP/1.0 200 OK"); //Send out a content is good/complete to make it look as though things have gone well
        
    header('Refresh: 1; url=http://spam.abuse.net/'); //polite redirect
          
    header("Connection: close");
          exit(); 
    }

    // check IP
    $user_ip $_SERVER['REMOTE_ADDR'];

        include_once (
    'baniplist.php');
        
        
    $check checkBan($user_ip);
        if (
    $check == 1)
        {
        echo 
    "Another error message here";
    die;
    }
     if (
    $check == 0)
     {
     
    // insert into database
    $q "insert into shoutbox (id,user_name,chat_body,user_email,date_time,user_ip) VALUES ('','$user_name','$chat_body','$user_email',now(),'$user_ip')";
    $result mysql_query($q);
    }
    if (
    $result)
    {
    // success so redirect user to view shoutbox.
    $siteurl 'http://www.mydomain.com/tagboard'// it's a wordpress template, hence the lack of "php" extension
    header('Location: '.$siteurl);
    }
    ?> 
    I obviously got rid of the unimportant stuff, like the spam words and the error messages, in order to post only the essential.
    When I test it, I get the first error message, which means the infos aren't passed through.

    So, I was thinking I could maybe put everything in one file, but I'm clueless to how to do that. I tried making my post.php content into a function and calling the function on submit, but it didn't work.
    I'd really appreciate all advice!

    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    I think I found out a solution! I'm not sure it's the best or anything, and I still would like to put everything in the same file (it would be easier to manage), but at least now it's working.

    No idea why, but on the last website, everything worked properly, and on this one, I had to add this:
    PHP Code:
    $user_name mysql_escape_string($_POST['user_name']);
    $user_email mysql_escape_string($_POST['user_email']);
    $chat_body mysql_escape_string(strip_tags($_POST['chat_body'])); 
    I put that at the beginning of my post.php file.

    I tried putting all together like that:

    HTML Code:
    <form method="post" action="<?php echo($_SERVER['PHP_SELF']) ?>">
    and then:
    PHP Code:
    function posttag () {
    {
        
    // the content of my post.php file here
    }
    if(isset(
    $_POST['submit']))
    {
       
    posttag();

    It didn't work, the data didn't go through (but it didn't output an error message either).
    Last edited by Ness_du_Frat; 11-24-2013 at 01:26 PM.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,360
    You are missing enctype="multipart/form-data" from your form tag.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    Thanks. What is it for, exactly? Is it really so bad to forget it? (I never put it in any of my forms)
    One more reason why I'd like everything in the same file is to get these cool ajax fade in error messages, instead of simply echoing the php ones. I've tried to find a tutorial for that, but I only get full scripts and such, and they are all very different, so it's difficult to learn from these.
    I only want to display the error message below the form. I'm not sure where to start.

  5. #5
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    Seems like I can't do anything with my error messages as long as my post.php is in another file... Since all the errors are handled in the post.php file, they open in their own page.
    Still looking for advice on how I can put everything in the same page (wordpress page, so no .php extension, and no direct link to the file possible since it's a template, so it will lead nowhere).
    Thanks!

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,360
    When posting a file or page, you need to tell the browser what it is that it is sending, is it binary data, plain text or data in a URL.

    Most post errors can be resolved with the addition of the enctype directive added.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles