简体   繁体   中英

Getting value from an input field Javascript

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

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

我只给它一个唯一的id值,例如id ='text1'并通过以下方式获取值:

document.getElementById('text1').value

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