How to Get value from PHP combobox?

Discussion in 'PHP' started by zer0c00l, Oct 28, 2010.

  1. #1
    I am in need of some help with my PHP and mySQL combobox. I have sucessfully created the dropdown box that is populated from a table called techs. Even that was a big achievement. The name of the column of the dropdown box is name.But now I need to post the value that is selected to "ajax.php", which then submits it to my db. I need to know how to modify the PHP code to get it to print the value to "ajax.php" when I hit the submit button. The other fields work just fine and post to the cutomers table.

    I have read as much as i could online, but could not find how to accomplish this task. Some have mentioned to add a hidden field, but am unsure how to do this. I am very new to php and sql, so any help is appreciated.


    It's just the PHP section that i really need some major help on.


    
    
    <form id="submit" action="ajax.php" method="post">
    
    <fieldset>
    
    <legend>LAPTOP CHECKOUT</legend>
    
    
    <label for="gid">Global ID:</label>
    
    <input id="gid" class="text" name="gid" size="4" type="text" />
    
    <br></br>
    
    <label for="fname">First Name:</label>
    
    <input id="fname" class="text" name="fname" size="20" type="text" /> 
    
    <br></br>
    
    <label for="lname">Last Name:</label>
    
    <input id="lname" class="text" name="lname" size="20" type="text" />
    
    <br></br>
    
    <label for="ophone"> Office Phone:</label>
    
    <input id="ophone" class="text" name="ophone" size="10" type="text" />
    
    <br></br>
    
    <label for="cphone"> Cell Phone:</label>
    
    <input id="cphone" class="text" name="cphone" size="10" type="text" />
    
    <br>
    
    <label for="email"> E-mail:</label>
    
    <input id="email" class="text" name="email" size="20" type="text" />
    
    
    
    
    HTML:
    
    
    <form>
    
    //Write out our query
    
    $query = "SELECT name FROM techs";
    
    
    $result = mysql_query($query) or die(mysql_error());
    
    
    
    $dropdown = "<select name='techs'>";
    
    
    while($row = mysql_fetch_assoc($result)) {
    
      $dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>";
    
    }
    
    
    $dropdown .= "\r\n</select>";
    
    echo $dropdown;
    
    PHP:

    
    <button class="button positive"> <img src="images/checkmark.png" alt="" /> Check Out Laptop </button>
    
    </fieldset>
    
        </form>
    
    HTML:
     
    zer0c00l, Oct 28, 2010 IP
  2. Griggs117

    Griggs117 Active Member

    Messages:
    73
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #2
    Actually submit your form to ajax.php... I'm not too confident that this method is safe at all.
     
    Griggs117, Oct 28, 2010 IP
  3. zer0c00l

    zer0c00l Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ajax.php is just a normal process.php page. I was originally using ajax script to submit, but removed it to make it simple for testing.

    this is the process page

    
    
    $username="*removed*";
    
    $password="*removed";
    
    $mysql_hostname = "*removed*";
    
    $database = "*removed*";
    
    
    
    // command to start database connection
    
    
    
    mysql_connect($mysql_hostname,$username,$password);
    
    
    
    // Select the database
    
    @mysql_select_db($database) or die( "Unable to select database");
    
    
    
    
    
    	// CLIENT INFORMATION
    
    	$gid          = htmlspecialchars(trim($_POST['gid']));
    
    	$fname        = htmlspecialchars(trim($_POST['fname']));
    
    	$lname        = htmlspecialchars(trim($_POST['lname']));
    
    	$ophone       = htmlspecialchars(trim($_POST['ophone']));
    
    	$cphone       = htmlspecialchars(trim($_POST['cphone']));
    
    	$email        = htmlspecialchars(trim($_POST['email']));
    
    	$cdate        = htmlspecialchars(trim($_POST['cdate']));
    
    	$rdate        = htmlspecialchars(trim($_POST['rdate']));
    
    	$ctech        = htmlspecialchars(trim($_POST['ctech']));
    
    
    
        $addClient  = "INSERT INTO customers (gid,fname,lname,ophone,cphone,email,cdate,rdate) VALUES ('$gid','$fname','$lname','$ophone','$cphone','$email','$cdate','$rdate')";
    
        mysql_query($addClient) or die(mysql_error());
    
    PHP:
     
    zer0c00l, Oct 28, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    According to your own code the drop-down box name='techs' and the closest reference to that on your collect POST data is $_POST['ctech']. I also cant find where your INSERT INTO customers statement, references 'ctech'

    So, I think you accidentally mixed names up and neglected to add 'techs' or 'ctech' to the sql statement ?
     
    Last edited: Oct 29, 2010
    MyVodaFone, Oct 29, 2010 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    Do a print_r($_POST)
     
    Kaizoku, Oct 29, 2010 IP
  6. zer0c00l

    zer0c00l Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ha ha yep you are right, I didn't have the ctech in the process.php page. I also didn't have it named right changed it to ctech. Thanks for the reply.



    I still can't get it though. The current code just populates the dropdown, I can't find anything online how to go about gettiing this to the process.php page.

    does this look better? I still can't get the value to submit into the mySQL table :(

    
    //Write out our query
    
    $query = "SELECT name FROM techs";
    
    
    
    
    
    $result = mysql_query($query) or die(mysql_error());
    
    
    
    $dropdown = "<select name='ctech'>";
    
    
    
    while($row = mysql_fetch_assoc($result)) {
    
      $dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>";
    
    }
    
    
    
    $dropdown .= "\r\n</select>";
    
    echo $dropdown;
    
    
    
    
    //the following section needs to pass the selected variable on to process.php page
    
    
    //How do i get the selected value into a variable?
    
    
    
    PHP:
     
    Last edited: Oct 29, 2010
    zer0c00l, Oct 29, 2010 IP
  7. zer0c00l

    zer0c00l Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I GOT IT! I made your corrections and I currently have it working!! Thank you so much!

    what do you mean not safe? is there a better way?
     
    zer0c00l, Oct 29, 2010 IP
  8. laxmanji

    laxmanji Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hi,
    The php programming language basically are developed in the website
    this language and this site are help to another person

    thank
    ................................................
     
    laxmanji, Oct 30, 2010 IP