dongyi6269 2014-05-26 21:33
浏览 47
已采纳

插入数据库动态生成的值

I have a simple PHP question.

I need to add inside a DB the result of this JavaScript code

function test() {
        $('#percentage').html(Math.floor((Math.random() * 100 + 1)) + '%');
    };

that generate inside a div a random number. I have a couple of textbox with some other infos and hey work properly, i just need to add this one but i can't.

the code to add the other results and then to display them is this:

ADDING ELEMENTS TO DB:

include('dbconnection.php');

$Name = $_POST['name'];
$Surname = $_POST['surname'];
$Random = $_POST['random'];
$Sentence =$_POST['sentence'];

$sql = "INSERT INTO `results`(`Name`, `Surname`, `Random`, `Sentences`) VALUES('$Name', '$Surname','$Random', '$Sentence')";

$retval = mysql_query( $sql );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}

header('Location: index.php');

DISPLAY DB RESULTS:

include('dbconnection.php');

         $result = mysql_query("SELECT `Name`, `Surname`, `Random`, `Sentences` FROM `results`");

         while ($row = mysql_fetch_array($result))
         {
            $name = $row['Name'];
            $surname = $row['Surname'];
            $random = $row['Random'];

            echo"Name: $name -- Surname: $surname -- Random: $random</br>";}

thanks!

  • 写回答

2条回答 默认 最新

  • doubo1883 2014-05-27 07:32
    关注

    You need to have a hidden variable in your form that keeps the value of number and then using this variable you can insert values in yoyr db when your form is posted.

    <input type="hidden" id="gen_value" name="gen_value" value=""/>
    

    and in the js function you need to change

    function test() {
            var calculatedValue=Math.floor((Math.random() * 100 + 1);
            $('#percentage').html(calculatedValue+'%');
            $('#gen_value').val(calculatedValue + '%');
        };
    

    changes in dbinsert code:

      include('dbconnection.php');
    
        $Name = $_POST['name'];
        $Surname = $_POST['surname'];
        $Random = $_POST['random'];
        $Sentence =$_POST['sentence'];
        $perCentage=$_POST['gen_value'];
    
        $sql = "INSERT INTO `results`(`Name`, `Surname`, `Random`, `Sentences`,`Percentage`) VALUES('$Name', '$Surname','$Random', '$Sentence',`$perCentage`)";
    
        $retval = mysql_query( $sql );
        if(! $retval )
        {
          die('Could not enter data: ' . mysql_error());
        }
    
        header('Location: index.php');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分