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

Asked

Viewed 399 times

2

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

I search this table with fecth_array So far so good but when I select one of the options I am not able to record and send the variable to the contact table. I ask for your help because I’ve been in this dilemma for days

Note: When I click the change button it is not sending the variable that the user selected but the last item of the array that is completed then even if I choose progress this taking the variable completed.

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>';
  • If you are starting in PHP start not using the mysql functions_*, Why should we not use mysql type functions_*?. do you send by link? or have some more code there?

  • thanks for the tip of the functions I will switch to PDO or msqli as suggested , yes use the link even passing the variables id and situ as shown in the last line .

  • There’s another while inside that?

  • Leonardo, either you use a form and a button to make a Ubmit, or you do as the Kaduamaral did in his reply. With form and a Ubmit button would be a little simpler.

4 answers

2

This happens because the while kept changing the variable $marca1 throughout the loop, when the variable ended was with the last value of the loop, example:

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

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

So you have to take the value of option 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 its variable $info[id] is out of quotes, at all times use quotes (simple) in array associative, example: $info['id'].

1

There are two life cycle types of a web application:

  1. Server side
  2. Client side

Since you want the link to the change to receive a value that changes according to your handling of the application on the Client side (in the browser), you should use Javascript to change the href of the link and not PHP, at least in the part that has to do with the Situation factor.

The code is already available in the other answers, so I’ll leave a reply using jQuery, below:

$(function () {

  $('#link').click(function (e) {
    e.preventDefault();
    var situ = $('#sitprot option:selected').val();
    var id = /*<?php echo (int)$info['id']; ?>;*/ 1;
    var newUrl = 'status1.php?id=' + id + '&situ=' + situ;
    if (situ != 0)
      /*location.href = newUrl;*/alert('redireciona para ' + newUrl);
    else
      alert('selecione situação, por favor');
  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form id='f' name='f'>
  <select id='sitprot' name='sitprot'>
    <option value='0'>[selecione um valor]</option>
    <option>esperando</option>
    <option>andamento</option>
    <option>concluído</option>
  </select>
</form>
<br />
<a href="#" id="link">Alterar</a>

1

Thanks First 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 'Alter';

then I put inside the head html tags the script

Document.getElementById('change'). addeventlistener('click', Function(Event){ Event.preventDefault(); var value = Document.getElementById('sitprot'). value; var link = 'status1.php? id=&situ='+value; Alert(value); Alert(link); and //Document.location.href = link; });

no error , but when I click change and seems not to enter the script and does not show Alert

tela

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

-1

Check if it works by replacing echo "<option value='$marca1'>$marca1</option>"; for
echo "<option value='{$marca1}'>{$marca1}</option>";

  • tried however gave in the same thing if I was using my option thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.