Reputation: 75
I'm trying to create a php form to change values in a file on my vps. Here is the code I am trying:
<?php
echo "<form ";
$type = (int)$_POST['animal_type'];
echo exec("sed -i 's/animal=*/animal=$type/g' /home/user1234/animals/file.props");
echo "method='post'>";
echo '<select name="animal_type" onChange="this.form.submit()">';
echo '<option value="bat">bat</option>';
echo '<option value="fish">fish</option>';
echo '<option value="cat">cat</option>';
echo '<option value="dog">dog</option>';
echo '</select>';
echo '</form>';
?>
I've updated my code again. Currently it just adds 00's to the animal= value in file.props instead of overwriting the current value with the value chosen in the form.
The value in the form on the php page also never changes and always stays as bat.
I also get this message every time I try to refresh the page: "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier."
Is there a better method for changing these values other than sed and exec? Or am I still doing something incorrect in my code?
Upvotes: 0
Views: 1333
Reputation: 2833
The action attribute of a form expects a URI to be specified. Instead, there is a exec function which returns a string. I'm assuming this string is not a valid URI. So in the source of the html it will look something like this:
<form action=some string returned by exec>
What is happening is the following:
I recommend that you simplify this by changing the form to post to another page <form action="changeFile.php">
then in changeFile.php add logic to modify the VPS file.
For example, in changeFile.php add logic to change the content with something like this:
<?php
exec("sed -i 's/animal=*/animal=$_REQUEST['type']/g' /home/user1234/animals/file.props");
echo 'file modified'
?>
This could also be accomplished by having the page post to itself with something like the following:
<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
if(!empty($_REQUEST['animal_type'])){
exec('sed -i '.escapeshellarg('s/animal=*/animal='.$_REQUEST['animal_type'].'/g')." /home/user1234/animals/file.props");
echo 'File has been updated';
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="animal_type">;
<option value="true">bat</option>;
<option value="false">fish</option>;
<option value="cat">cat</option>;
<option value="dog">dog</option>;
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
Upvotes: 1