简体   繁体   中英

Trying to send the value of a radio button in a form to a php variable.

I tried the following code, and I can't figure out what I'm doing wrong. I know I'm breaking rules by having the form spread out amongst two different divs, but I don't know any other way around it.

<?php
echo '<form name="form" method="POST">';
$directory = '/var/www/admin/html/content';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
echo 'Files<br>';
while($it->valid()) {
    if(!$it->isDot()) {
        echo '<input type="radio" name="file_picked" value="content/' . $it->getSubPathName() . ' " id="file_picked" />' . $it->getSubPathName() . '</br>';
    }
    $it->next();
}

echo '<input type="submit" name="pickedName" value="Edit File" /></div>
<div class="editor">
<h1>SS Code Editor</h1>';

$file_picked = $_POST['file_picked'];
$edit_field  = $_POST['edit_field'];

if(isset($_POST['pickedName'])) {
    //get file contents and display in textarea box
    $theData = file_get_contents($file_picked);
    echo "<textarea name=\"edit_field\" id=\"edit_field\" cols=\"100\" rows=\"60\">";
    echo $theData;
    echo "</textarea><br />";
}

if(isset($_POST['submitChanges'])) {
    //grab new textarea contents and put into file.
    $theData = file_put_contents($file_picked, $edit_field);

    //redraw textarea with new contents
    $theData = file_get_contents($file_picked);
    echo "<textarea name=\"edit_field\" id=\"edit_field\" cols=\"100\" rows=\"60\">";
    echo $theData;
    echo "</textarea><br />";
}
?>
<input type="submit" name="submitChanges" value="Save">
</form>

You have an extra space at the end of the checkbox input value :

Replace :

value="content/' . $it->getSubPathName() . ' " id="...

with :

value="content/' . $it->getSubPathName() . '" id="...

So file_get_contents($file_picked = $_POST['file_picked'])) don't find any file (with space at the end) and returns false, which is displayed as "" in the textarea.

您的值应存储在$ _POST ['file_picked']中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM