PHP $ _POST [' $ var']获取动态php表单的名称

时间:2015-08-31 15:15:56

标签: php html5

//this is the php code that i call in another file

    if ( isset($_POST['star'])){
        $check_vote = $_POST['star'];

        $new_rate = 0;
        ***$video1_id = $_POST["$video_id"];*** //the problem is here
        $find_video = mysqli($con, "SELECT * FROM videos WHERE video_id='$video_id1'");

        // and this is the html code that another php function echoes//

form name='$video_id' method='POST'                             
input class='star star-5' id='star-5' type='radio' name='star' value='5'/>                              
<label class='star star-5' for='star-5'></label>                                
<input class='star star-4' id='star-4' type='radio' name='star' value='4'/>                         
<label class='star star-4' for='star-4'></label>                            
<input class='star star-3' id='star-3' type='radio' name='star' value='3'/>                             
<label class='star star-3' for='star-3'></label>
<input class='star star-2' id='star-2' type='radio' name='star' value='2'/>
<label class='star star-2' for='star-2'></label>
<input class='star star-1' id='star-1' type='radio' name='star' value='1'/>
<label class='star star-1' for='star-1'></label>    
<input type='submit' name='$video_id' value='Vote' id='$video_id'>                  
</form> 

// my problem is that i want to take the variable $video_id(either form the form either from the submit-button) to search for the specific video but i cant. i tried $video1_id = $_POST["$video_id"]; and $video1_id = $_POST['$video_id']; and this $video1_id = $_POST[$video_id]; and this $video1_id = $_POST['.$video_id'];

thnx你的时间

2 个答案:

答案 0 :(得分:0)

你不能像$_POST["$video_id"]那样获得变量的值,而是从一个带有文本或隐藏字段的字段发送它,并将其另一面。

此外,您必须从html划分您的PHP代码。 “$ blabla”是语法错误。 将php打开和关闭标签添加到你的php之类的东西,比如

所以你可以在html中使用你的php代码

例如

<h1><?=$video_id?> </h1>

<input type="hidden" name="vID" value="<?=$video_id?>" />

并且像

一样
$myVideoId = $_POST["vID"];

答案 1 :(得分:0)

// php echo html代码中的解决方案就是这个......

form name='$video_id' method='POST'                             
input class='star star-5' id='star-5' type='radio' name='star' value='5'/>                              
label class='star star-5' for='star-5'></label>                                
input class='star star-4' id='star-4' type='radio' name='star' value='4'/>                         
label class='star star-4' for='star-4'></label>                            
input class='star star-3' id='star-3' type='radio' name='star' value='3'/>                             
label class='star star-3' for='star-3'></label>
input class='star star-2' id='star-2' type='radio' name='star' value='2'/>
label class='star star-2' for='star-2'></label>
input class='star star-1' id='star-1' type='radio' name='star' value='1'/>
label class='star star-1' for='star-1'></label>    
input type='submit' name='$video_id' value='Vote' id='$video_id'>                  
/form

//其他函数中的正确php代码就是这个

if(isset($ _ POST [&#39; star&#39;])&amp;&amp;&amp; isset($ _ POST [&#39; video_id&#39;])){             $ check_vote = $ _POST [&#39; star&#39;];

        $new_rate = 0;
        $video_id = $_POST['video_id'];
        $sql = " SELECT * FROM videos WHERE video_id = '$video_id' ";
        $find_video = mysqli_query($con, $sql);