如何通过PHP在MySQL中插入HTML选择值作为文本

如何通过PHP在MySQL中插入HTML选择值作为文本

问题描述:

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

我有这个选择字段: ` p>

 &lt  ; select class =“large-fld”id =“regioni”name =“region”value =“”&gt; 
&lt; option value =“0”hidden&gt; City ...&lt; / option&gt; 
&lt; option value  =“775”&gt; Tbilisi&lt; / option&gt; 
&lt; option value =“776”&gt; Kutaisi&lt; / option&gt; 
&lt; option value =“788”&gt; Batumi&lt; / option&gt; 
等等......  
  code>  pre> 
 
 

现在我需要,当我在PHP中调用这个选择'region' code>然后在MySQL中插入值。
例如:有人选择Tbilisi(即 $ _ POST ['region'] = 775 code>),此值(775)将转到DB。 我需要,PHP从相当于这个值的HTML中选择775而不是数据库插入TBILISI ...
nny clue? p>

提前感谢... p> \ n div>

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']];

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;

}

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.