James
James

Reputation: 132

javascript get/store data from table

I'm trying to catch the data based on the id's after clicking the button (like store them) in order to use it for inserting needed id's afterwards into the DB. The js written is not working as expected, any ideas what is missing? Thank you for the support.

   	

<script>  
  function goo() {
		idprod=$("#idprod").val();
		nprod=$("#nprod").val();
		lastprod=$("#lastprod").val();
		solarprod=$("#solarprod").val();
		locprod=$("#locprod").val()
	}
</script>
<form name="goo" method="post">	
<table border="1">
	<tr>
		<th>ID</th>
		<th>Name</th>
		<th>Lastname</th>
		<th>WK_Solar</th>
		<th>Location</th>
		<th>Save</th>
	</tr>
   	<tr>	
		<td id="idprod">P1</td>
		<td id="nprod">James</td>
		<td id="lastprod">Lee</td>
		<td id="solarprod">$1555</td>
		<td id="locprod">Queens</td>
		<td><input type="hidden" name="active" value="active"><input type="submit" name="submit" value="goo"></td>
	</tr>
	<tr>	
		<td id="idprod">P2</td>
		<td id="nprod">Marc</td>
		<td id="lastprod">Hoobs</td>
		<td id="solarprod">$955</td>
		<td id="locprod">Bronx</td>
		<td><input type="hidden" name="active" value="active"><input type="submit" name="submit" value="goo"></td>
	</tr>
</table>
</form>

Upvotes: 0

Views: 84

Answers (1)

Alen.Toma
Alen.Toma

Reputation: 4870

1- You are using val which is the attr Value. you should use html() or text() to get the value.

2- You have two rows, you should browse those two rows and get the data from each row

 function goo() {
    var firstRow = $(".test tbody tr").last();
		idprod=firstRow.find("#idprod").html();
		nprod=firstRow.find("#nprod").html();
		lastprod=firstRow.find("#lastprod").html();
		solarprod=firstRow.find("#solarprod").html();
		locprod=firstRow.find("#locprod").html()
    
    console.log(idprod)
	}
  $(document).ready(function(){
  
   goo();
  });
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="goo" method="post">	
<table border="1" class="test">
<thead>
	<tr>
		<th>ID</th>
		<th>Name</th>
		<th>Lastname</th>
		<th>WK_Solar</th>
		<th>Location</th>
		<th>Save</th>
	</tr>
</thead>
   	<tr>	
		<td id="idprod">P1</td>
		<td id="nprod">James</td>
		<td id="lastprod">Lee</td>
		<td id="solarprod">$1555</td>
		<td id="locprod">Queens</td>
		<td><input type="hidden" name="active" value="active"><input type="submit" name="submit" value="goo"></td>
	</tr>
	<tr>	
		<td id="idprod">P2</td>
		<td id="nprod">Marc</td>
		<td id="lastprod">Hoobs</td>
		<td id="solarprod">$955</td>
		<td id="locprod">Bronx</td>
		<td><input type="hidden" name="active" value="active"><input type="submit" name="submit" value="goo"></td>
	</tr>
</table>
</form>

Upvotes: 1

Related Questions