Jump to content

Newbie Form Help!


zrweber

Recommended Posts

Ok on my page (index.php) I have 3 numbers.

1.
2.
3.

I want people to be able to fill out a form then depending on what number they select they're name will be on that number..

So like if someone choosed option 2 on the form and their name was Bob it'd look like this

1.
2. Bob
3.

Then someone else signs it and chooses option 1 and their name is John

1. John
2. Bob
3.

How would I do this? Also how can I make it to where people cannot choose the same number? So if 1 is taken people cannot choose it.

Thanks
Link to comment
https://forums.phpfreaks.com/topic/26004-newbie-form-help/
Share on other sites

you would need a database of some sort with a list of all the numbers and the assigned names etc.
use mysql and create a table then add a field called ID and have it as an auto_increment then make a field called number and another called user. then in your php form you could just make something like this:

[code]
<?php

$connect = mysql_connect("localhost","user","pass");
mysql_select_db("db");

if(isset($_POST['submit'])){ //checks if they have submitted our form

}
?>
<form action="index.php" method="POST">
Your Name: <input type="text" name="name"><br>
Number: <select name="number">
<?php
$query = mysql_query("SELECT * FROM table WHERE `user`=''"); //this selects all the numbers from table that dont have users assigned to them.
if(mysql_num_rows($query) == 0){
echo "<option value=''>There are no numbers left</option>"; //if there are no numbers, an option with that text will be displayed.
}else{

while($r = mysql_fetch_array($query)){ //selects all the rows
$number = $r['number'];
echo "<option value='".$number."'>Number ".$number."</option>"; //this shows all the options with Number ??
}
}
?>
</select><br>
<input type="submit" name="submit" value="Add Me!">
</form>
Current Users:<br>
<?php
$query2 = mysql_query("SELECT * FROM table"); //selects ALL the numbers
while($r2 = mysql_fetch_array($query2)){
$number = $r2['number'];
$user = $r2['user'];

echo "#".$number.". ".$user."<br>"; //displayed the following: #??. Username
}
?>
[/code]

that should b your index.php. that should do it. i hope. good luck.
Link to comment
https://forums.phpfreaks.com/topic/26004-newbie-form-help/#findComment-118965
Share on other sites

Did you populate the DB with the numbers you want to use? If you didn't then this query would be empty.
Would that not not cause your "There are no numbers left" to come up. Just guessing here, I a new to PHP

$query = mysql_query("SELECT * FROM table WHERE `user`=''");\

Cheers!
Stephen

Link to comment
https://forums.phpfreaks.com/topic/26004-newbie-form-help/#findComment-119817
Share on other sites

you could easily do it in mysql with a while command and just run it once to populate it then you should be fine.

example:
[code=php:0]
$connect = mysql_connect($host, $user, $pass);
mysql_select_db($db);

for($i = 0; $i < 100; $i++){ //this will run until i is 100
mysql_query("INSERT INTO table (`number`) VALUES ('".$i."')");
}
[/code]

Then your done. this would be a lot quicker then manually doing it in phpMyAdmin.
good luck.
Link to comment
https://forums.phpfreaks.com/topic/26004-newbie-form-help/#findComment-120182
Share on other sites

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.