ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to make a webpage that, through the use of php, will let me enter some information into forms and pass it on to a bash script.
I have a really simple page working where I can enter information into a form and submit it, and I am able to run a bash script through php. I am also able to print out what was entered in the form, but when I try to run the bash script and give it what was entered in the form, I only get the variable name, not its contents.
the whole thing goes like this
text from form goes to bash script ---> bash script relays this to a file to be checked ---> file contains only the word name.
this is my index.html with the forms
---------------------------
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</body>
</html>
this is my test.php that relays the form's stuff to the bash script
-----------------------------
<html>
<body>
<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('test.sh' . " " . '$_POST["name"]');
echo 'You entered ' . $_POST["name"] . "!";
?>
</form>
</body>
</html>
the bash script IS executing and passing the first argument to it into a file I can check. But instead of the file containing what you entered in the form, it only contains "name". What throws me off is that I can print the contents of name in the webpage just fine.
in short:
echo exec('test.sh' . " " . '$_POST["name"]'); ends up sending just "name" to test.sh
echo 'You entered ' . $_POST["name"] . "!"; prints the contents of name.
how is this possible? how do i get the contents of name passed into the bash script?
In php the double quote is a suggestion to the interpreter to expand any variable, whilst the single quote will never expend any variable. Thus $i = 7; echo '$i'."=$i"; will result in $i=7 being displayed. Things get a little more complicated when you have arrays, since (if I recall correctly) the interpreter will expand the first thing that it can hence, "$POST['name']" would be expanded to Array['name'], since $POST is an array. To force the array element to be expanded in double quotes you can include them in braces thus "{$POST['name']}". This means that you exec call could be exec("test.sh {$_POST['name']}");
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.