dongshen3352 2017-02-28 21:58
浏览 48
已采纳

将表单值传递给函数以获取数组值

I have read several tutorials to try and get my head around this problem. I have my code working fine with the text input box, but I'm trying to pass the radio box to my function as well and use it to get my array value.

I will implement things like form validation and sanitisation later, but now I just want to be able to output the array result within my function, depending on what radio button was selected. This is the code I have tried:

index.php

<?php 
if (isset($_POST['submit'])) {
   $data = $_POST['name']; // the data from text input.
   $colour = $_POST['colour']; // data from colour radio.
}
?>
...
<form action="/app.php" method="post">
<input type="text" name="name">
<input name="colour" type="radio" value="1">Red
<input name="colour" type="radio" value="2">Blue
<input name="colour" type="radio" value="3">Green
<input type="submit" name="submit" value="Submit">
</form>
 <img src="pngfile.php?data=<?php print urlencode($data);?>">

pngfile.php

<?php
    require_once 'functions.php';  
   $textdata = urldecode($_GET['data']);
   $colourdata = urldecode($_GET['colour']);
   process($textdata,$colourdata);
   exit;
?>

functions.php

<?php

/* Generate Image */
function process($textdata, $colourdata)
{

$colour_array = [
    "1" => "#9E2A2B",
    "2" => "#3E5C76",
    "3" => "#335C67",
];
...

I am stuck with the piece of the code that will take the radio button value (1/2/3) and then look up the equivalent array value and output the colour, i.e: if radio button with value 1 is selected, then we output 1#9E2A2B.

  • 写回答

1条回答 默认 最新

  • dsvbtgo639708 2017-02-28 22:09
    关注
    <form action="/app.php" method="post">
    

    form method POST not GET

    $_GET['colour'] edit to $_POST['colour']
    

    in function

    function process($textdata, $colourdata)
    {
    
    $colour_array = [
        "1" => "#9E2A2B",
        "2" => "#3E5C76",
        "3" => "#335C67",
    ];
    
    $color = isset($colour_array[$colourdata]) ? $colour_array[$colourdata] : false;
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM