Jump to content

comparing $_Post vars so no Duplicate


chiefrokka

Recommended Posts

I can figure it out with some for loops and what not, but just wondering if there's a function that does this for you already. 

 

I have 12 html list menu's that are INT's between 1-12 they can select... when they submit, I need to varify they didn't choose the same numbers for two different list menu's. 

 

basically I need the html form fields to be Unique.  maybe there's a tag to add to the html form instead or is there a php function to test?

Link to comment
https://forums.phpfreaks.com/topic/121470-comparing-_post-vars-so-no-duplicate/
Share on other sites

Ha, woops, thought you said checkboxes.  That's not how you should name them though.  You should name them ALL 'pick[]', and then $_POST['pick'] will be a multidimensional array containing all the choices.  You can then use array_unique, like this:

 

if (!($_POST['pick'] === array_unique($_POST['pick'])) {

  //duplicate choices!

}

your right, I should probably be using array's for this crap.  I keep forgetting to use arrays, maybe because I'm not too familiar with them yet.  thanks

"The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed."

 

I don't want to remove them though but I just want to check if duplicates exist so i can prompt the user to go back and fix their choices. 

I'm playing with multi arrays for first time and have a question now.  I have an array set up that print_r works just fine so I store their ranks from the html form properly... they have 12 TeamNames they have to rank from best to worst.  The TeamName is defined already by me.

 

$Team[1][TeamName][Rank]

$Team[2][TeamName][Rank]

$Team[3][TeamName][Rank]

 

so when they fill out the html form the only thing their choosing is the rank.  so my array could be like this based on their ranks they chose.

$Team[1][Patriots][2]

$Team[2][Colts][1]

$Team[2][Cowboys][3]

 

How do I echo let's say the [TeamName] based on a for loop for the [Rank].  I also need to know how to loop through all 12 ranks and update my database with that TeamName

 

<?php
for ($i=1; $i<=12; $i++)
{		
     echo "<br>Rank #".$i." = ".$Team["Rank"=$i].""; // How do I grab their #1 ranked team?
     // Rank #1 = TeamName
     // Rank #2 = TeamName

     $str= "Rank_".$i."";	// variable name in database = Rank_1, Rank_2, etc.
     // how do I update database for that TeamName     
     mysql_query("UPDATE `Users` SET `$str` = '".$Team[$i]["Team"]."', Already_Picked='1' Where `Name` = '$user' ") 
or die(mysql_error());
}
?>

 

 

that would be easier to do with a javascript onsubmit() clause rather than php validation, it is easy to do with php, but a far excess and unneccessary waste of time and effort, onsubmit you need to check if document.getElementById('pickN').value is the same as picknn etc.... you can do that quite quickly with a loop and then document.location.href if it passes the validation

 

 

-T`L

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.