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

时间:2017-02-28 21:58:00

标签: php arrays function

我已经阅读了几个教程,试图解决这个问题。我的代码在文本输入框中工作正常,但我也试图将单选框传递给我的函数并使用它来获取我的数组值。

我稍后会实现表格验证和消毒等功能,但现在我只想在我的功能中输出数组结果,具体取决于所选的单选按钮。这是我尝试过的代码:

的index.php

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

pngfile.php

<?php

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

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

的functions.php

1#9E2A2B

我遇到了将采用单选按钮值(1/2/3)的代码段,然后查找等效数组值并输出颜色,即:如果选择值为1的单选按钮,然后我们输出<script> var request = new XMLHttpRequest(); request.onreadystatechange = function() { if(request.readyState === XMLHttpRequest.DONE) { if(request.status === 200) { //PERFORM ANY ADDITIONAL ACTIONS AFTER A SUCCESSFUL POST HERE... } else { alert('An error occurred. Please try again.'); } } } var xyz = ""; xyz = "Cookies Enabled: " + navigator.cookieEnabled; document.getElementById("demo").innerHTML = xyz; if (navigator.cookieEnabled) { xyz = ""; request.open('POST', 'http://yourpostbackurl.com/enabled'); request.send(); } else { request.open('POST', 'http://yourpostbackurl.com/disabled'); request.send(); xyz = "Cookies seem to be disabled in your browser."; } document.getElementById("demo").innerHTML = xyz; </script>

1 个答案:

答案 0 :(得分:1)

<form action="/app.php" method="post">

表单方法POST不要GET

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

在功能

function process($textdata, $colourdata)
{

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

$color = isset($colour_array[$colourdata]) ? $colour_array[$colourdata] : false;
...