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
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
Share on other sites

Thanks for responding!

It's not really working, did I make the table wrong?

[code]CREATE TABLE `numberselect` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`number` VARCHAR( 31 ) NOT NULL ,
`user` MEDIUMTEXT NOT NULL ,
PRIMARY KEY ( `ID` )
) TYPE = innodb;[/code]
Link to comment
Share on other sites

I'm not getting any errors, I gave ID a value and it's still not wanting to work for me.

Hmm, sorry I must be doing somethin wrong I'm a big noob at PHP I just started to learn it last month. I just need a form like this to put it on this guys website who needs it
Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.