0

I have a form which fills the input fields on page load off of MySQL server, but when I try to assign it's value to a JavaScript variable, it crashes.

Here is JS;

 function getTotal(e)
  {
    total = myform2.1.value;

  }

It just crashes and here is my html/php;

    <?php
include('connect.php');
$id=$_GET['id'];
$result = $db->prepare("SELECT * FROM zones WHERE id= 1");
$result->bindParam('1', $id);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>

<form action="edit.php" method="POST" class="elegant-aero" name="myform2">
<input type="hidden" name="memids" value="<?php echo $id; ?>" />
<br>
<input type="text" name="1" value="<?php echo $row['1']; ?>" /><br>
<br>
<input type="text" name="2" value="<?php echo $row['2']; ?>" /><br>
<br>
<input type="text" name="3" value="<?php echo $row['3']; ?>" /><br>
<input type="button" value="za" id="secret" onclick="getTotal(this)"/>
</form>
<?php
    }
?>

Basically I fill the HTML input fields with data from MySQL but the JavaScript variable total didn't couldn't retrieve the value, although the HTML input field gets the value just fine from the MySQL server via PHP, any thoughts??

SoftwareEnthusiast
  • 271
  • 2
  • 9
  • 26

2 Answers2

2

The problem is with myform2.1.value;. Javascript doesn't understand why you're having number (1) after a dot (myform2.).

Best solution would be to rename the fields so that their names don't begin with a number but rather with a letter (such as name="input1" etc.)

leopik
  • 2,323
  • 2
  • 17
  • 29
0

I would just give it a unique id value such as id='text1' and get the value by:

document.getElementById('text1').value