1

I have this select field: `

<select class="large-fld" id="regioni" name="region" value="" >
<option value="0"  hidden>City...</option>
<option value="775">Tbilisi</option>
<option value="776">Kutaisi</option>
<option value="788">Batumi</option>
and so on...

Now I need, when I will call this selection 'region' in PHP and then insert value in MySQL.
For example: Someone choose Tbilisi (i.e. $_POST['region']=775) and this value (775) goes to DB. I need, PHP choose from HTML equivalent of this value and instead 775 in DB insert TBILISI...
any clue?

Thanks in advance...

3 Answers3

0

Are 775>Tbilisi, 776>Kutaisi static data in html? Simply you can create an array like

$regions = array('775'=>'Tbilisi','776'=>'Kutaisi');

and before processing form substitute values:

$region = $regions[$_POST['region']];
  • ok... but if there is a huge select field? whole world for example, or many people? your method works (perhaps) but not effective :) must be something command in php, which sees Tbilisi from select field or some method, like xml parser in python, which sees whole html and gives you full control. – Manuelo Zumbadze Feb 01 '16 at 00:49
  • It is better to move all data from selectbox to db, or to a big array oO. And no. PHP takes an _POST array [key=>value] without html code. You can do ajax request with any data you wish, but with default POST request you must have some data to compare – Igor Goroun Feb 01 '16 at 00:53
  • and how it would be with AJAX? – Manuelo Zumbadze Feb 01 '16 at 01:02
  • You can find complex description here http://stackoverflow.com/questions/9436534/ajax-tutorial-for-post-and-get or http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – Igor Goroun Feb 01 '16 at 01:05
  • But it seems it is better to create an array of key=>values – Igor Goroun Feb 01 '16 at 01:07
  • Spasibo :) baza ne ochen bolshoi i uje peredelal po tvoemu sovetu – Manuelo Zumbadze Feb 01 '16 at 01:09
0

Set up a switch statement with all the values listed and their corresponding names. The major advantage of this is that you have all possible "safe" values already listed, therefore if a user tries to use SQL injection, they won't be able to attack you because it will revert to your default case. However the disadvantage is that if there are lot of fields, you will have to list them all. I'm pretty sure there might be a way to hack the system, to get "innerHTML" property with the POST request, but I'm not sure.

$region = $_POST["region"];

switch($region){

    case "775":
       $insert = "Tbilisi"
    break;
    ...//more cases

     default:
     //if no case matches, do something here

     break;

}
David Yue
  • 713
  • 7
  • 24
0

I don't know if you still need this info, but: 1. first of all, why don't you use the select option like this?

<option value="Tbilisi">Tbilisi</option>
<option value="Kutaisi">Kutaisi</option>

Then the Php will take the name directly.

  1. If for any reason, the option above is not possible, I would also suggest jQuery

use this (to post your value -as region name - to you php script):

$var = $( "#regioni option:selected" ).text();
$.post( "your-php-script.php", { region: $var } );

this code above should be included in an event handler (onclick, onselect...) depending what you need. read more about jquery, you need it.

John_Dean
  • 37
  • 7