Pass Mysql <option> values

Asked

Viewed 424 times

1

Guys I’m having a hard time to perform a Submit of a specific field in case the <select> with 3 <option>. When I get the field to enter some data in the database it does not bring the value, but the information Undefined or Object ( From what I understand it brings the data Undefinido or Object ) In the way the code is it is not imputing the data. Follow the code below:

   function updatedata(str){

var id = str;
var nm = $('#nm'+str).val();
var gd = $('#gd'+str).val();
var pn = $('#pn'+str).val();
var al = $('#al'+str).val();
var nt = $('#nt'+str).val();
var nq = $('#nq'+str).val();
var me = $('#me'+str).val();
var sc = $('#sc option:select'+str).text();
var nr = $('#nr'+str).val();
var ob = $('#ob'+str).val();

var datas="nm="+nm+"&gd="+gd+"&pn="+pn+"&al="+al+"&nt="+nt+"&nq="+nq+"&me="+me+"&sc="+sc+"&nr="+nr+"&ob="+ob;

$.ajax({
   type: "POST",
   url: "php/updatedata.php?id="+id,
   data: datas
}).done(function( data ) {
  $('#info').html(data);
  viewdata();
});
}

Input text fields work perfectly, but only select box that is not functional. Follow select in html.

<div class="form-group">
    <label for="sc">status:</label>
    <select class="form-control" id="sc" >
        <option value='1'>Sim</option>
        <option value='2'>Não</option>
        <option value='3'>Revisar</option>
    </select>

I had also put html values as text instead of numbers, but there was also no input.

Complementing this information are passed to an Update set via modal, if I change any field within the modal is performed the input but only the value of the <select> who is not fooled.

Grateful from now on!

Guys, I was able to input the data with the answer

var sc = $('select option:selected').text();

But there was a problem that it replicates the information of according to the number of bank lines, example:

If the bank has 5 records it will add Simsimsimsim and not only Yes and if I update the information with a No it will Naosimsimsimsim.

Thank you if you can help me, I will post the full code with html and php, no longer know what to do to solve this problem.

Grateful.

 <script>
    function viewdata(){
       $.ajax({
	   type: "GET",
	   url: "php/getdata.php"
      }).done(function( data ) {
	  $('#viewdata').html(data);
      });
    }
    $('#save').click(function(){
	
	var nm = $('#nm').val();
	var gd = $('#gd').val();
	var pn = $('#pn').val();
	var al = $('#al').val();
	var nt = $('#nt').val();
	var nq = $('#nq').val();
	var me = $('#me').val();
	var sc = $('#sc').val();
	var nr = $('#nr').val();
	var ob = $('#ob').val();
	
	var datas="nm="+nm+"&gd="+gd+"&pn="+pn+"&al="+al+"&nt="+nt+"&nq="+nq+"&me="+me+"&sc="+sc+"&nr="+nr+"&ob="+ob;
      
	$.ajax({
	   type: "POST",
	   url: "php/newdata.php",
	   data: datas
	}).done(function( data ) {
	  $('#info').html(data);
	  viewdata();
	});
    });
    function updatedata(str){
	
	var id = str;
	var nm = $('#nm'+str).val();
	var gd = $('#gd'+str).val();
	var pn = $('#pn'+str).val();
	var al = $('#al'+str).val();
	var nt = $('#nt'+str).val();
	var nq = $('#nq'+str).val();
	var me = $('#me'+str).val();
	var sc = $('select option:selected').text();
	var nr = $('#nr'+str).val();
	var ob = $('#ob'+str).val();
	
	var datas="nm="+nm+"&gd="+gd+"&pn="+pn+"&al="+al+"&nt="+nt+"&nq="+nq+"&me="+me+"&sc="+sc+"&nr="+nr+"&ob="+ob;
      
	$.ajax({
	   type: "POST",
	   url: "php/updatedata.php?id="+id,
	   data: datas
	}).done(function( data ) {
	  $('#info').html(data);
	  viewdata();
	});
    }
    function deletedata(str){
	
	var id = str;
      
	$.ajax({
	   type: "GET",
	   url: "php/deletedata.php?id="+id
	}).done(function( data ) {
	  $('#info').html(data);
	  viewdata();
	});
    }
    </script>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<table class="table table-bordered table-hover">
	<thead>
	  <tr>
	    <th>ID</th>
	    <th>Contrato</th>
	    <th>Carteira</th>
	    <th>Data Pagamento</th>
	    <th>Valor Pagamento</th>
	    <th>Parcelamento</th>
	    <th>Quitação</th>
	    <th>Envio</th>
	    <th>Status</th>
	    <th>Recuperador</th>
	    <th>Observação</th>
	  </tr>
	</thead>
	<tbody>
   
	  <tr>
	    <td><?php echo $row['id_acordo']; ?></td>
	    <td><?php echo $row['ncontrato']; ?></td>
	    <td><?php echo $row['ncarteira']; ?></td>
	    <td><?php echo $row['dpagamento']; ?></td>
	    <td><?php echo $row['vpagamento']; ?></td>
	    <td><?php echo $row['nparcelamento']; ?></td>
	    <td><?php echo $row['nquitacao']; ?></td>
	    <td><?php echo $row['menvio']; ?></td>
	    <td><?php echo $row['scadastro']; ?></td>
	    <td><?php echo $row['nrecuperador']; ?></td>
	    <td><?php echo $row['observacao']; ?></td>
		
	    <td>
	    <a class="btn btn-warning btn-sm" data-toggle="modal" data-target="#myModal<?php echo $row['id_acordo']; ?>"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
	    <a class="btn btn-danger btn-sm"  onclick="deletedata('<?php echo $row['id_acordo']; ?>')" ><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>

<!-- Modal -->
<div class="modal fade" id="myModal<?php echo $row['id_acordo']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel<?php echo $row['id_acordo']; ?>" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel<?php echo $row['id_acordo']; ?>">Editar Linha</h4>
      </div>
      <div class="modal-body">

<form>
  <div class="form-group"> 
    <p id="nm<?php echo $row['id_acordo']; ?>">
      <b>Contrato:&nbsp;</b><?php echo $row['ncontrato']; ?>
    </p>
  </div>

  <div class="form-group">
    <p id="gd<?php echo $row['id_acordo']; ?>">
      <b>Carteira:&nbsp;</b><?php echo $row['ncarteira']; ?>
    </p>
  </div>

  <div class="form-group">
    <p id="pn<?php echo $row['id_acordo']; ?>">
      <b>Data:&nbsp;</b><?php echo $row['dpagamento']; ?>
    </p>
  </div>

  <div class="form-group">
    <p id="al<?php echo $row['id_acordo']; ?>">
      <b>Valor:&nbsp;</b><?php echo $row['vpagamento']; ?>
    </p>
  </div>

  <div class="form-group">
    <p id="nt<?php echo $row['id_acordo']; ?>">
      <b>Parcelamento:&nbsp;</b><?php echo $row['nparcelamento']; ?>
    </p>
  </div>
  
  <div class="form-group">
    <p id="nq<?php echo $row['id_acordo']; ?>">
      <b>Quitação:&nbsp;</b><?php echo $row['nquitacao']; ?>
    </p>
  </div>

  <div class="form-group">
    <p id="me<?php echo $row['id_acordo']; ?>">
     <b>Envio:&nbsp;</b><?php echo $row['menvio']; ?>
    </p>
  </div>

  <div class="form-group">
    <p id="nr<?php echo $row['id_acordo']; ?>">
      <b>Recuperador:&nbsp;</b><?php echo $row['nrecuperador']; ?>
    </p>
  </div>
  
  <div class="form-group">
		<label for="sc">status:</label>
		<select class="form-control" id="sc">
			<option>Sim</option>
			<option>Nao</option>
			<option>Revisar</option>
		</select>
  </div>
    
 <div class="form-group">
    <label for="ob">Observação:</label>
    <input type="text" class="form-control" id="ob<?php echo $row['id_acordo']; ?>" value="<?php echo $row['observacao']; ?>">
  </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
        <button type="button" onclick="updatedata('<?php echo $row['id_acordo']; ?>')" class="btn btn-primary">Salvar</button>
      </div>
    </div>
  </div>
</div>
</form>	    
	    </td>
	  </tr>
<?php
}
?>
	</tbody>
      </table>

2 answers

1

You’re using the selector incorrectly.
You must use #sc option:selected and not #sc option:select'+str

To know the value just use $('#sc').val(), to know the text of this option then you must do so: $('select option:selected').text().

jsFiddle: https://jsfiddle.net/t8tqjLow/

  • or $(#sc).val() also works! D

  • @Marllonnasbe exact, I have it in the answer :)

0

Since you use jQuery use find to find the selected option like this:

var itemSelecionado = $('select').find(":selected");

document.write(" <br> Texto do Item Selecionado :" + itemSelecionado.text());
document.write(" <br> Valor do Item Selecionado :" + itemSelecionado.val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<select class="form-control" id="sc">
  <option value='1'>Sim</option>
  <option value='2'>Não</option>
  <option value='3'>Revisar</option>
</select>

Browser other questions tagged

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