0

私のページには、ユーザーに身長/体重を入力するように求めるフォームがあり、BMI を計算してデータベースに保存します。javascript 変数を php 変数に転送する方法がわかりません。非表示のフォームを使用する必要があることは理解していますが、それらがどのように機能するのかわかりません。

ここに私のコードがあります

<?php include "base.php"; ?>
<?php session_start (); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">    
<head>    
<title>BMI</title>  
</head>

<body>

<form id="bmi" action="main.php" method="post">
<p>Enter your height(in inches):</p><input type="text" id="height box" name="height box">
<p>Enter your weight(in pounds):</p><input type="text" id="weight box" name="weight box">
<input type="hidden" id="bmi" name="bmi">
<input type="button" value="Calculate" id="calc button" name="calc button" onClick="calc()">
</input>
</form>

<script type="text/javascript">

function calc()
{
    // get variables from textboxes
    var height=document.getElementById('height box').value;
    var weight=document.getElementById('weight box').value;
    var bmi=document.getElementById('bmi').value;

    // calculate BMI
    weight/=2.2;
    height/=39.37;
    BMI=Math.round(weight/(height*height));

</script>

<?php

//insert username, date, and bmi into the db
$username = mysql_real_escape_string($_SESSION['Username']);
$date = date('Y-m-d');
$bmi = mysql_real_escape_string($_POST['bmi']);

mysql_query("INSERT INTO bmi (username, bmi, date) VALUES('".$username."', '".$bmi."', '".$date."')");

?>

</body>

</html>

base.php は、SQL サーバーに接続してデータベースのものを選択する場所です。

JavaScript の bmi 変数が php の $bmi 変数に転送されていないようです。私が間違っているのは何ですか?

4

4 に答える 4

2

非表示入力の値をbmi計算値に設定します。

<form id="bmi" action="main.php" method="post">
<p>Enter your height(in inches):</p><input type="text" id="height box" name="height box">
<p>Enter your weight(in pounds):</p><input type="text" id="weight box" name="weight box">
<input type="hidden" id="bmi" name="bmi">
<input type="submit" value="Calculate" id="calc button" name="calc button" onClick="calc()">
</form>

<script type="text/javascript">

function calc()
{
    // get variables from textboxes
    var height=document.getElementById('height box').value;
    var weight=document.getElementById('weight box').value;
    var bmi=document.getElementById('bmi').value;

    // calculate BMI
    weight/=2.2;
    height/=39.37;
    document.getElementById('bmi').value=Math.round(weight/(height*height));
}
</script>
于 2013-08-04T23:01:26.700 に答える
1

あなたのコードに見られるいくつかの問題:

  1. HTML では、スペースidを含む属性を持つことはできません( http://www.w3.org/TR/html4/types.html#type-id )name
  2. を計算していますがbmi、フォーム要素に割り当てていません。
  3. calc()JavaScript関数に右中括弧がありません。

解決策は、最初に id 属性を修正することです。たとえば、次のようなキャメル ケース表記を使用できます。

<form id="bmi" action="main.php" method="post">
<p>Enter your height(in inches):</p><input type="text" id="heightBox" name="heightBox">
<p>Enter your weight(in pounds):</p><input type="text" id="weightBox" name="weightBox">
<input type="hidden" id="bmi" name="bmi">
<input type="submit" value="Calculate" id="calcButton" name="calcButton" onClick="calc()">
</form>

次のように JavaScript を更新します。

function calc()
{
    // get variables from textboxes
    var height=document.getElementById('heightBox').value;
    var weight=document.getElementById('weightBox').value;
    var bmi=document.getElementById('bmi');

    // calculate BMI
    weight/=2.2;
    height/=39.37;
    bmi.value=Math.round(weight/(height*height));
}

注意すべきもう 1 つの点は、mysql_非推奨の拡張機能を使用していることです。代わりにmysqliまたはPDOの使用を開始します。

于 2013-08-04T23:07:40.303 に答える
0

理解を深めるために、コードを 3 つの部分に簡略化しました。

HTML: URL の変数を表示するGET代わりに、メソッドを使用しています。閉じ括弧がないPOSTこともご了承くださいinput</input>

<form action="index.php" method="GET">
    <input type="text" name="height">
    <input type="submit" onclick="calc();">
</form>

JavaScript: window.location.href URL に変数を表示するようにブラウザーに指示します。

function calc(){
    var height = document.getElementById('height').value;
    window.location.href = "index.php?height=" + height; 
}
</script>

PHP:を使用して変数を取得できるようになりました$_GET['height']

<?php
    $height = $_GET['height'];
    echo $height;
?>
于 2013-08-04T23:21:01.407 に答える