The selected value of the select variable $ val ['myid'] is transferred from the form, I need to pass the $ matn variable from the form, but this variable must be passed along with the selected option.

<form method="POST" name="apple" id="apple" action="tabl.php" target="_blank"> <select class="selectpicker" name="user" id="user" data="<?=$matn?>" > <option disabled selected >Выбрать сотрудника</option> <? foreach($row as $k=>$val){ $matn=$val['mname']; $matn=substr($matn,0,-6); ?> <option value="<?=$val['myid']?>" id="link" > <?=$val['mname']?> </option> <? } ?> </select> </form> 

  • Make it into a hidden input, and when you post the form, it will be transferred. - Moonvvell
  • The select contains 30 values, the variable input is also 30 values, if I add a hidden field, the last element is transmitted. And I need to pass the value selected from the selector - Eliot
  • For each select $ matn? Or for each option? If the second one, for example, connect them into one variable with a delimiter - for example, $ val ['myid'] ___ $ math and send this to the server, then separate yourself. These are all options without javascript - Moonvvell
  • And yes, what is the variable input you are talking about? Add to the question all the information that can help in helping you - Moonvvell
  • yes, for each opton its own value as for the variable $ matn - Eliot

1 answer 1

Composite key option without js.

  <form method="POST" name="apple" id="apple" action="tabl.php" target="_blank"> <select class="selectpicker" name="user" id="user" data="<?=$matn?>" > <option disabled selected >Выбрать сотрудника</option> <? foreach($row as $k=>$val){ $matn=$val['mname']; $matn=substr($matn,0,-6); ?> <option value="<?=echo $val['myid'] . "/". $matn ?>" id="link" > <?=$val['mname']?> </option> <? } ?> </select> </form> 

On the server we do:

 $user = explode("/", $_POST['user']); 

The first element is id, the second is name

Option with js:

 <form method="POST" name="apple" id="apple" action="tabl.php" target="_blank"> <input type="hidden" name="user_name" value="1"> <select class="selectpicker" name="user" id="user" data="<?=$matn?>" > <option disabled selected >Выбрать сотрудника</option> <? foreach($row as $k=>$val){ ?> <option value="<?=$val['myid']?>" id="link" > <?=$val['mname']?> </option> <? } ?> </select> </form> 

 function setName(elem) { var text = elem.options[elem.selectedIndex].text; text = text.substr(0, text.length - 6); document.getElementById('user_name').value = text; } 
 <input id="user_name" name="user_name" type="hidden" /> <select name="user" onchange="setName(this)"> <option value="1">Алексей123456</option> <option value="2">Василий123456</option> </select> 

Well, on the back already get from the post $_POST['user_name'] $_POST['user']

  • Fully decided my question, thank you. One question remained, in the first version I get the variable $ user as an array and how to make the variable? The fact is that this $ user variable I will put in the code and look for a file on a server with a similar name. code example echo "<pre>"; if(file_exists($_SERVER['DOCUMENT_ROOT']."/project/$user.sql")){ $sql = file_get_contents($_SERVER['DOCUMENT_ROOT']."/project/$user.sql"); } else echo "Нет данного файла"; echo $sql; echo "</pre>"; echo "<pre>"; if(file_exists($_SERVER['DOCUMENT_ROOT']."/project/$user.sql")){ $sql = file_get_contents($_SERVER['DOCUMENT_ROOT']."/project/$user.sql"); } else echo "Нет данного файла"; echo $sql; echo "</pre>"; - Eliot
  • one
    you can do so $userArr = explode("/", $_POST['user']);$user = $userArr[0]; $userName = $userArr[1]; $userArr = explode("/", $_POST['user']);$user = $userArr[0]; $userName = $userArr[1]; And then work on with c $ user and $ userName - Kostiantyn Okhotnyk