我正在使用选择表单元素,询问用户他想要使用哪个函数。
我的问题实际上如下:
1表单元素:
<form action="php/questhandler.php" method="post" parsley-validate="" novalidate="">
<div class="col-sm-12 col-md-12">
<div class="form-group">
<label>Quest</label>
<select name="inputQuest" id="inputQuest" class="form-control">
<option value="0">Please select a quest...</option>
<option value="1">PHPcode1</option>
<option value="2">PHPcode2</option>
</select>
</div>
</div>
<div style="margin-left:11px;" class="form-group">
<button class="btn btn-primary" type="submit">Redeem Quest</button>
</div>
</form>
PHP文档
<?php
$choose = $_POST['inputQuest'];
// SELECTION 1
if($choose = '1'){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'POSTURLHERE');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'POSTFIELDSHERE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $global['cookie']);
$response = curl_exec($ch);
curl_close($ch);
echo "FUNCTION 1 SUCCESSFULL";
}
// SELECTION 2
if($choose = '2'){
$startValue = 0;
$endValue = 10;
for($startValue; $startValue <= $endValue; $startValue++){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'POSTURLHERE');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'POSTFIELDSHERE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $global['cookie']);
$response = curl_exec($ch);
curl_close($ch);
echo "FUNCTION 2 got " .$startValue. " times added.";
}
}
?>
我的问题如下:我如何让PHP代码只运行从表单元素发布的选定代码?
我尝试使用if参数,因为你们可以在代码中看到,但它仍然会运行这两个代码。
答案 0 :(得分:2)
使用$choose == '1'
和$choose == '2'
- 这种方式是检查变量是否与值完全匹配