??yy 2015-01-28 17:23 采纳率: 0%
浏览 23

PHP生成POST表单

I generate a form based on some data I get from a database.

Here is my constructor:

php_core.php

<?php
    echo "<style>";
    echo ".spoll{ -webkit-box-sizing: border-box;";
    echo "-moz-box-sizing: border-box;";
    echo "box-sizing: border-box;";
    echo "padding: 10px;";
    echo "padding-top: 5px;";
    echo "}";
    echo "</style>";

    //DISPLAY FUNCTION

    function display($x){
        require("db_conx.php");
        $sql = "SELECT * FROM polls WHERE id='$x' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
            $poll_id = $row['id'];
            $topic = $row['topic'];
            $startdate = $row['startdate'];
            $stopdate = $row['stopdate'];
            $active = $row['active'];
            $type = $row['type'];
            $title = $row['title'];
            $pwidth = $row['pwidth'];
            $borcolor = $row['borcolor'];
            $borwidth = $row['borwidth'];
            $fcolor = $row['fcolor'];
            $bfcol = $row['bfcol'];
            $bg = $row['bg'];
            $borrad = $row['borrad'];
            $abars = $row['abars'];
            $bcol = $row['bcol'];
            $brad = $row['brad'];

        }

            //DISPLAY THE POLL
            $aquery = "SELECT * FROM answers WHERE parent='$poll_id'";
            $result = mysqli_query($db_conx, $aquery);
            echo '<div class="spoll" style="width: '.$pwidth.'px; border: '.$borwidth.'px solid '.$borcolor.'; border-radius: '.$borrad.'px; color: '.$fcolor.'; background-color: '.$bg.';">';
            echo '<p><b>'.$title.'</b></p>';
            echo '<p style="font-size: 13px;">'.$topic.'</p>';
            echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';

            // SELECT ANSWERS
            if($type == 'single'){
                while ($usersRow = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    echo '<input type="radio" name="rad" value="'.$usersRow['id'].'"/>'.$usersRow['content'].'<br><br>';
                }
            } else {
                $i = 0;
                while ($usersRow = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    echo '<input type="checkbox" name="check[]" value="'.$usersRow['id'].'"/>'.$usersRow['content'].'<br><br>';
                    $i++;
                }
            }

            //echo '<button style="border-radius: '.$brad.'px; border: none; padding: 10px; width: 100%; margin: auto; background-color: '.$bcol.'; color:'.$bfcol.'" value="VOTE" onclick="vote('.$poll_id.');"><b>VOTE</b></button>';
            echo '<input type="submit" value="Vote" style="border-radius: '.$brad.'px; border: none; padding: 10px; width: 100%; margin: auto; background-color: '.$bcol.'; color:'.$bfcol.'">';
            echo '</form>';
            echo '</div>';
    }

    // END OF THE DISPLAY FUNCTION


?>

Here is how I display the form:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include("php_includes/php_core.php"); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php display('15'); ?>
<span id="status"></span>
<?php 
if(isset($_POST['submit'])){
echo "Posted!";
?>
</body>
</html>

I don't know why, but it won't submit the values! The function outputs the form correctly, I've checked it by viewing the page source. I have no idea why it does not work, so I need your help. I've been modifying the script again and again, no result.

An additional(optional) question about this script: Do you have any idea about how I can submit this form using AJAX without any additional HTML tags and refresh the function output without reloading the page? I found some ways, but I must wrap the function inside a div and I need to keep the output structure and method, so I'm out of ideas.

  • 写回答

2条回答 默认 最新

  • weixin_33730836 2015-01-28 17:30
    关注

    It doesn't submit the form because you have not set any name to the button.

    if(isset($_POST['submit'])){
    
    }
    

    submit doesn't exists.

    Change to:

    echo '<input type="submit" name="submit">';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法