+ Reply to Thread
Results 1 to 9 of 9

Thread: Stop Form Values Being Equal

  1. #1
    ChrDav is offline x10Hosting Member ChrDav is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    15

    Stop Form Values Being Equal

    Hi everyone,

    I am trying to stop form values being equal to each other. My form contains about 30 different SELECT/OPTION combinations. I want none of the select fields to have the same option selected when Submit is pressed. These values (assuming none are equal) are then stored in a SQL database.

    My only thought so far is to have repeated 'if' combinations checking every combination but I am sure there must be a better solution.

    Thanks,
    Chris.

  2. #2
    sybregunne is offline x10Hosting Member sybregunne is an unknown quantity at this point
    Join Date
    Feb 2008
    Posts
    54

    Re: Stop Form Values Being Equal

    A suggestion would be to use javascript create an array. every time an option is selected shortlist the array you may use an index, mark off those array elements that have been selected.

    Of course you may use checkboxes to make sure each selection is unique.

  3. #3
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: Stop Form Values Being Equal

    well, there's a couple of ways.

    one would be making an array storing all the field names, the other would be having field values being the same, just with a number in brackets at the end form[1], form[2]. as far as html goes, it's 2 field names, but php can handle those names easily.

    as for the nitty gritty, i'm not gonna get into it now, because it's late and i have to get up early for work tomorrow, and have been having a hard time doing so. and i'm working outside all day (all week long so far) and it's a little cold in delaware ;)

  4. #4
    dickey's Avatar
    dickey is offline x10 Sophmore dickey is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    128

    Re: Stop Form Values Being Equal

    xplozion could you care to elaborate on that second method. I want to apply this on my site too. but I was thinking more of using javascript but when you suggest php I think that would be better.

    Thanks...
    Don't get me wrong as I believe if and when I help someone I also help myself whereby whatever someone learns I also learn.

    But I will also accept credits or reps if you really want to part with it.

  5. #5
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: Stop Form Values Being Equal

    ok, it's been a while, but the forum software i use (fluxbb) uses the same method, but not for comparing data. give me a bit and i'll try to work on a method (also designing new site), since i've never actually did this, but with arrays and either foreach'es or for's, it shouldn't be too hard.

    here's my brainstorm though:

    since the way the input name's are written test[1], test[2], etc... it's already built an array. from there, use the foreach to go through one by one for the main string defined by foreach, and if there's a match, return 0, making sure to make it not catch the same form and return on that (test[1] == test[1] return 0) ;). if there's no match, return 1 (which will make it easy for if(check($blah)) {

    PHP Code:
    function form_check($name)
    {
      foreach (
    $name AS $k => $v)
      {
        foreach(
    $name AS $n => $c)
        {
          if(
    $v == $c) && ($k != $n)
          {
            return 
    0;
          }
        }
      }
      return 
    1;
    }

    //usage

    if (form_check($_POST['test']))
    {
      
    //no matches
    }
    else
    {
      
    //matches

    might be some errors, but i didn't test this, just wrote it out on here

    -xP
    Last edited by xPlozion; 01-15-2009 at 01:15 PM.

  6. #6
    ChrDav is offline x10Hosting Member ChrDav is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    15

    Re: Stop Form Values Being Equal

    Ah yes, foreach loops, of course.

    I'll look at it in a couple of days when I get some free time, thanks xPlozion

  7. #7
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: Stop Form Values Being Equal

    np, just let me know if there's any major problems that you cant fix yourself

    if it works, please give rep (that goes for you too dickey) ;)

  8. #8
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: Stop Form Values Being Equal

    Just an FYI, if you're expecting string values then you should use === for comparison. However in this case, array_unique() would be faster than making a function:

    PHP Code:
    if (count(array_unique($_POST['values'])) == count($_POST['values'])) {
        
    //no matches
    }
    else {
        
    //matches

    But if you're expecting numeric data, then you'll have to use a function like the one xPlozion provided. This is because array_unique() checks if the values are *identical* as strings. So "1.0" wouldn't match "1".
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  9. #9
    ChrDav is offline x10Hosting Member ChrDav is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    15

    Re: Stop Form Values Being Equal

    I just spent some time playing around with the code. xPlozion, thanks heaps! I've modified your code slightly to suit my uses better but the foreach loops was the key. There was just one little error:
    Quote Originally Posted by xPlozion View Post
    PHP Code:
          if($v == $c) && ($k != $n
    It should read:
    PHP Code:
          if(($v == $c) && ($k != $n)) 
    It was only reading the ($v == $c) as the if statement and ignoring the second part.

    Thanks,
    Chris.

+ Reply to Thread

Similar Threads

  1. stop new year
    By neteater in forum Forum Games
    Replies: 10
    Last Post: 01-01-2009, 04:54 AM
  2. Addon Domain PHP Form Error
    By rockee in forum Free Hosting
    Replies: 2
    Last Post: 04-02-2008, 06:53 PM
  3. MySQL Issues Here
    By Corey in forum Service Alerts
    Replies: 304
    Last Post: 01-06-2008, 09:10 PM
  4. A simple Visual Basic Login Form
    By Zenax in forum Tutorials
    Replies: 0
    Last Post: 03-13-2007, 08:59 AM
  5. Mod Help
    By Ericsson in forum Free Hosting
    Replies: 4
    Last Post: 03-04-2005, 03:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers