Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Unable to get the selected item text in php

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Unable to get the selected item text in php

Postby stephenrodrics » Mon Mar 18, 2013 3:06 am

I have one code where on one page i auto fill 20 combo boxes, these comboboxes are auto selected based on one PARENT combo.

now my problem is this is a form when user clicks submit, i have to read the value displayed in 20 combos and just enter into Database.

my problem is when i have 20 combos in 1 php page called Page1.php.

When user clicks Submit button on Page1.php users is moved to Page2.php where all Database Insert query is written, there in that 2nd page how can i read the 20 combox value based on the index.

below is my code

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$combo1 = $_POST['combo1'];

echo $combo1; out put is None

$combo10=$_POST['combo10'];

//$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)


echo "Comboxvalue is" ;

echo $combo10; Output is None
$sql="INSERT INTO $tbl_name(username, password) VALUES ('s2','sp')";

$res = mysql_query($sql);
$count=mysql_num_rows($res);

echo "count is ";

echo $count;

if($res==1){

echo "Success";

}
else
{
echo "Please check the selected records are valid or not.. ";

}
?>

even though for 1st and 10th combo I have selected TH99 but still the value comes None, which is the default value

echo $combo10; Output is None


see the image attached
Last edited by stephenrodrics on Fri Mar 22, 2013 2:00 am, edited 2 times in total.
stephenrodrics
New php-forum User
New php-forum User
 
Posts: 58
Joined: Thu Mar 14, 2013 6:10 am

Re: Unable to get the selected item text in php

Postby seandisanti » Mon Mar 18, 2013 8:04 am

1) I've answered how to read the inputs in your other post. make sure you're specifying unique name properties on each select box.
2) the mysql_ functions are slow and insecure. PDO is the best way to handle database access right now, and you can get a free quick crash course at http://jream.com/learning/videos/php-oo ... o-examples (i think it runs about 12 minutes long but it will make your life and your code a lot easier)
3) using constants or variables for your connection variables is a good idea, but when you have them saved in the same file, it really defeats any real benefit. a good practice is to create a database class that loads its connection variables as constants from another file. Then if credentials ever change, you have one uncluttered file to edit, and you avoid having your database credentials in every file that needs a database connection.

Here's a freebie, it's the Database.php that I am using for my current project. It is included as needed by an autoloader, and whenever I need a database connection, I just use $db = Database::getInstance(); and as the code shows, it returns the existing instance if there is one, or it creates a new one if there isn't one. The connection variables are also being included in my header file, by way of a file called constants.php that contains nothing but the declarations of DBHOST, DB, DBUSER, and DBPASSWORD.
Code: Select all
<?php
/*
 * Database class only one connection is allowed.
 */

class Database extends PDO{

    private 
$connection;
    private static 
$instance;

    public static function 
getInstance() {
        if (!
self::$instance) {
            
            
self::$instance = new self("mysql:host="DBHOST ";dbname=".DB,DBUSER,DBPASSWORD);
        }
        return 
self::$instance;
    }



    
/*
     * empty clone magic method to prevent duplication
     *
     */

    
private function __clone() {

    }

    public function 
getConnection() {
        return 
$this->connection;
    }

}
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Unable to get the selected item text in php

Postby stephenrodrics » Fri Mar 22, 2013 1:59 am

Thanks dear it solved my issue
stephenrodrics
New php-forum User
New php-forum User
 
Posts: 58
Joined: Thu Mar 14, 2013 6:10 am

Re: Unable to get the selected item text in php

Postby seandisanti » Fri Mar 22, 2013 7:05 am

No problem, I'm glad to help
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.