简体   繁体   中英

dynamically get hidden field value without submitting form

my query is generating 3 result with three send enquiry button which send the value of there respective hidden field but all the time when i click send enquiry button i show me always the 1st company name in my javascript

here is my php..

  $adcategory=$_GET['category'];
  mysql_connect('localhost','root','');
  mysql_select_db('advertising site');
  $selectquery="select * from business_ads where company_category='$adcategory'";
  $result=mysql_query($selectquery)or die(mysql_error());
  $resultrows=mysql_num_rows($result);
  if($resultrows>=1)
  {
  echo"<p>$resultrows</p>";
  while($row=mysql_fetch_array($result))
  {
  $companyname=$row['company_name'];?>
  <div id="ads">
  <p id="company_name" name="company_name"><?php echo $companyname?></p>
  <input type="hidden" name="hidden_company_name" value="<?php echo        
  $companyname>"id="hidden_name">   
  <input type="hidden" name="hidden_company_name" value="<?php echo $companyname ?    
  >"id="hidden_name">
  <center><input type="button" name="send_enquiry" id="send_enquiry" value="Send    
  Enquiry"       
  onclick="f1();"></center>
  <input type="submit"value="submit" name="submit_enquiry_form">

 </div>
 <?php
 }
 }

and here is my javascript

 function f1()
 {
 var comp_name=document.getElementById('hidden_name').value;
 var elements=comp_name;
 document.write(elements);
 }

Both these show there is no space between your closing quote and the id

<input type="hidden" name="hidden_company_name" value="<?php echo        
$companyname>"id="hidden_name">   
<input type="hidden" name="hidden_company_name" value="<?php echo $companyname ?    
>"id="hidden_name">

Also it is bad coding to have more than one element with the same ID. As you will never end up with the proper element when you try to reference it by ID.

If this is just going to be a hidden value that is not changed by javascript then simply doing this will provide the value into a js var.

<script type="text/javascript">
var valueIwant="<?php echo $companyname; ?>";
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM