Select Option does not search or save the value selected by the user

1

I am trying to get the value selected by the user in a option days ago and there is no way I am not able to use two tables a contact call and another situation, in the situation I have the values waiting, progress and completed.

I do a search in this table with fecth_array until then, but when I select one of the options I'm not sure how to write and send the variable to the contact table. I ask your help because I am already in this dilemma a few days

Note: when I click on the change button it is not sending the variable that the user selected, but the last item in the array that is finished so even though I choose progress this is leading to the completed variable.

echo'<td><select name="sitprot" id="sitprot">';
$sql="select * from situacao"; // neste permite  mostrar e alterar e as opções para ser gravado no banco.
$resultado2=mysql_query($sql);
while($dados1=mysql_fetch_array($resultado2)){
    $marca1=$dados1['situ'];
    echo "<option value='$marca1'>$marca1</option>";
    echo $marca1;
}
echo '</select> <td>';   
echo '<td><a href="status1.php?id='.$info[id].'& situ='.$marca1.'">Alterar</td>';
    
asked by anonymous 16.09.2015 / 21:52

3 answers

0

Make sure it works by substituting echo "<option value='$marca1'>$marca1</option>"; for echo "<option value='{$marca1}'>{$marca1}</option>";

    
17.09.2015 / 15:58
0

This happens because while was changing the variable $marca1 throughout the loop , when finished the variable got the last value of loop , example:

$i = 0;
while($i++ < 10){
   $marca = $i;
}

echo $marca; // Resultado: 9 - último valor dentro do while

So, you have to get the value of option with Javascript:

<select name="sitprot" id="sitprot">
  <option value="1">Valor 1</option>
  <option value="2">Valor 2</option>
  <option value="3">Valor 3</option>
  <option value="4">Valor 4</option>
</select>
<a href="#" id="alterar">Alterar</a>
<script type="text/javascript">
document.getElementById('alterar').addEventListener('click', function(event){
  event.preventDefault();
  var valor = document.getElementById('sitprot').value;
  var link = 'status1.php?id=<?=$info["id"]?>&situ='+valor;
  alert(valor);
  alert(link);
  //document.location.href = link;
});
</script>

Note: I noticed that your $info[id] variable is unquoted, always use single quotes in associative array , example: $info['id'] . >     

17.09.2015 / 16:19
0

Thank you Firstly for the good support

Below I put your code inside php so I used echo

echo '';   echo 'Value 1';   echo 'Value 2';   echo 'Value 3';   echo 'Value 4';   echo '';   echo 'Change';

Then I put the head html tags inside the

  document.getElementById ('alter'). addEventListener ('click', function (event) {    event.preventDefault ();   var value = document.getElementById ('sitprot'). value;   var link = 'status1.php? id = & situ =' + value;   alert (value);   alert (link);   //document.location.href = link; });

There was no error, but when I click on change and it does not appear to enter the script and does not show the alert

<select name="sitprot" id="sitprot">
  <option value="1">Valor 1</option>
  <option value="2">Valor 2</option>
  <option value="3">Valor 3</option>
  <option value="4">Valor 4</option>
</select>
<a href="#" id="alterar">Alterar</a>
<script type="text/javascript">
document.getElementById('alterar').addEventListener('click', function(event){
  event.preventDefault();
  var valor = document.getElementById('sitprot').value;
  var link = 'status1.php?id=<?=$info["id"]?>&situ='+valor;
  alert(valor);
  alert(link);
  //document.location.href = link;
});
</script>
    
18.09.2015 / 15:09