I am a hobbyist with limited knowledge of html, javascript, php. I set up a Raspberry Pi microcomputer as a Web server. As a starting point I want to turn on a LED that is connected to the Pi with a button on a Web page. I have the following code that works (name of html file is min.php):
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LED Control</title>
</head>
<body>
LED Control:
<form method="get" action="min.php">
<input type="submit" value="ON" name="on">
</form>
<?php
$setmode7 = system("gpio mode 7 out");
if(isset($_GET['on'])){
$gpio_on = system("gpio write 7 1");
echo "LED is on";
}
else {
echo "LED is off";
}
?>
</body>
</html>
Now I want to rewrite the code with an ajax function so that the page does not reload when the button is clicked and here I have problems. I looked at a lot of the posted examples but I just can't get over the hump. I changed the html code as follows:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LED Control</title>
</head>
<body>
<button type="button" onclick="LED_On()">LED On</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"><?script>
<script>
function LED_On(){
$.ajax({
url:"LED_On.php",
type:"GET",
data:"on"
});
}
</script>
</body>
</html>
The LED_On.php file has the following code and is stored in the same directory as the html file:
<?php
if(isset($_GET['on'])){
$setmode7 = system("gpio mode 7 out");
$gpio_on = system("gpio write 7 1");
echo "LED is on";
}
else {
echo "LED is off";
}
?>
Clicking the button does not turn on the LED. Any help is appreciated.
Check your code
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"><?script>
<?script> should be changed to </script>
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I recently start the php programming. However, I encountered a problem about running a function on php file. My php file is listed as follows:
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="Test()" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
function Test()
{
echo "Goodbye";
}
?>
</body>
</html>
After running the program, a webpage is observed with A button entitled submit and the word "Hello". However, After click on the button, the webpage "This page cannot be displayed" is observed. But I respect the word "Goodbye" is shown. I transferred the php code to another file, but the problem was not resolved.
actions in forms aren't functions like JavaScript. You are trying to run Test() on submit which isn't a thing. What you want is action to be action=" "
In reality you should be doing something like this:
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
if(isset($_POST['submit'])){
echo "Goodbye";
}
?>
</body>
</html>
If you WANT to use a function....
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
function test(){
echo "Goodbye";
}
if(isset($_POST['submit'])){
test();
}
?>
</body>
</html>
Lets say he file name is test.php where you have put all your code so the action values should be test.php
<form method="post" action="test.php" >
this is the code for my html page in the site:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
</head>
<body>
<html>
<body>
<form action="Untitled-4.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
</body>
</html>
and this is my code for the "Untitled-4.php" file:
<html>
<body>
<?php
if ($_GET["name"] == "tom"){
echo "hello you are special";
}
?>
</body>
</html>
this is the error i am getting:
"undefined index on line 7 of the php file"
I am trying to use the GET method to take whatever the user types in the "name" box to be used in the if statement that echoes "you are special" if the user types in "tom". can anyone tell me what the problem here is.
(the question stack overflow is saying is a possible duplicate is a completely different question)
Check if the variable is set first:
<html>
<body>
<?php
if (isset($_GET["name"])) {
if ($_GET["name"] == "tom"){
echo "hello you are special";}
} else {
echo "you are not special";
}
?>
</body>
</html>
for some reason i just tried it again with no changes to my code and it appears to be working i did not actually have to do anything.
I am working on a php app and this is my code:
<html>
<head>
<meta charset="UTF-8" />
</head>
<?php
if (isset($_POST['PLAY']))
{
exec("open /Applications/Chess.app");
}
?>
<form method="post">
<button name="PLAY">Play Chess</button><br>
</form>
</html>
But when I run it, and press the button the Application does not open. Thanks for any help!
There is an space before /Applications/Chess.app
try
exec("open/Applications/Chess.app");
I just learned to use form in CodeIgniter. I had learn it before, but run on Windows. And now, I'm trying on Ubuntu. I had followed user_guide, but when I was running it, there was no change on the page.
Here are the view named myform.php
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<input type="text" name="name" value=""/>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
the other form named formsuccess.php
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h3>Your form was active!</h3>
<p><?php echo anchor('form', 'Try it again!'); ?></p>
</body>
here are the controller named form.php
function index() {
$this->load->helper(array('form'));
$this->load->library('form_validation');
if ($this->form_validation->run() == FALSE) {
$this->load->view('myform');
} else {
print_r('good luck!');
}
}
when I click on submit button, it still show myform.php means the form is not working. Can you see the problem? Is there any difference between running CodeIgniter on Windows and Ubuntu?
I'm sorry for the stupid question. Because there is a note on user guide that I wasn't see. "If you submit the form you should simply see the form reload. That's because you haven't set up any validation rules yet."
Very simply, I'm creating a PHP content database for a website. I want to create buttons next to a text field, with a title drawn from a table. Clicking this button then inserts an equivalent code into the field. These PHP table consists of three columns: key, word and code
So far I have the following code... this DOES populate the button value, but the does not insert the code. I suspect this is due to the use of '' within the PHP code. Am I correct?
Appreciate any guidance
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function insertText(elemID, text)
{
var elem = document.getElementById(elemID);
elem.innerHTML += text;
}
</script>
</head>
<body>
<form>
<textarea id="txt1"></textarea>
<input type="button" value="<?php echo $row_tooltips['word']; ?>" onclick="insertText('txt1', '<?php echo $row_tooltips['code']; ?>');">
</form>
</body>
</html>
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function insertText(elemID, text)
{
var elem = document.getElementById(elemID);
elem.innerHTML += text;
}
</script>
</head>
<body>
<form>
<?php
$row_tooltips['word'] = "buttontitle";
$row_tooltips['code'] = "code";
?>
<textarea id="txt1"></textarea>
<input type="button" value="<?php echo $row_tooltips['word']; ?>" onclick="insertText('txt1', '<?php echo $row_tooltips['code']; ?>');">
</form>
</body>
</html>
Changed nothing of your code but predefining the $row_tooltips array and it works completely fine for me. Are you sure, that this array gets filled correctly?
greetz
So you say:
What I'm trying to achieve is a button to insert some HTML code though so when the user hits a button a full link is included into a PHP text field.
Then indeed, if there is an ' in the string that has been echoed, your script breaks. Also with any other htmlspecialchars, especially " your run into problems; So you need to escape that.
Also, htmlcode inside a textarea is standard not possible, so you should probably make it a div (with an textbox inside, if you need that).
Based on your original code, below a code that works ok. I use htmlspecialchars for the plain text, and addslashes for your html with (the ', as encoding them does not work).
<?php
// Remove this, only for testing
$row_tooltips['word'] = "Insert a FooBar";
$row_tooltips['code'] = "<h1>Delicious 'FooBar'</h1><img src=\"http://thumbs.dreamstime.com/x/reep-chocolade-12835946.jpg\" alt=\"foobar\" />";
?>
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function insertText(elemID, text)
{
var elem = document.getElementById(elemID);
elem.innerHTML += text;
}
</script>
</head>
<body>
<form>
<div id="txt1"></div>
<input type="button" value="<?php echo htmlspecialchars($row_tooltips['word'], ENT_QUOTES, 'ISO-8859-1'); ?>"
onclick="insertText('txt1', '<?php echo addslashes(htmlspecialchars($row_tooltips['code'], ENT_COMPAT, 'ISO-8859-1')); ?>');">
</form>
</body>
</html>
Tested in Chrome.