HTML Button to Run PHP Script

Hello All,

I have three php script, 1.php, 2.php and 3.php.

I want to call these scripts using 3 HTML buttons, can any one help me with a simple HTML button code to call php script

Regards,
Anantha
AnanthahkAsked:
Who is Participating?
 
vrynxzentConnect With a Mentor Commented:
inside index.php

<?php include '1.php'; ?>      

<input type="button"> onclick="<?php THE SCRIPT FROM 1.php; ?>">
0
 
Shinesh PremrajanConnect With a Mentor Engineering ManagerCommented:
Pls refer the attached snippet

hope this helps
<body>
<input type="button" onclick="1.php" vaue="Calling One" name="one"/>
<input type="button" onclick="2.php" vaue="Calling Second" name="Second"/>
<input type="button" onclick="3.php" vaue="Calling Third" name="third"/>

</body>

Open in new window

1
 
Mehul_PanchalConnect With a Mentor Commented:
hi,

you have to give button onlick method to go fro pariticular page.

<input type="button1" onclick="window.location=webrootpath/1.php"/>
<input type="button2" onclick="window.location=webrootpath/2.php"/>
<input type="button3" onclick="window.location=webrootpath/2.php"/>

Thanks
Mehul


0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Mark BradyConnect With a Mentor Principal Data EngineerCommented:
Mehul has it almost correct. It needs to be like this:

<input type="button1" onclick="window.location.href = 'path/1.php'"/>
<input type="button2" onclick="window.location.href = 'path/2.php'"/>
<input type="button3" onclick="window.location.href = 'path/3.php'"/>

You should put single quotes around the URL part ie: 'path/1.php'
window.location.href is the correct way to reference an external file for navigation to it.
0
 
AnanthahkAuthor Commented:
Hello Everybody

Thanks for your suggestion, I'm quite novice to any scripting so please excuse my questions

First solution (index.php) has some couding issue, I'm not able to run the script using php I get following error
 
# php index.php
PHP Parse error:  syntax error, unexpected T_STRING in /usr/local/apache2/htdocs/index.php on line 3

Second solution I just get 3 buttons but on clicking it does not execute the php script

Third sol : I get just three input text box on web page

This is the HTML script I'm using currently to call  1.php, 2.php & 3.php script.

Its works but runs the php  in new page thus remove the buttons, Can I display the output in same page ??

Or please advise if any changes required in the script.

HTML Code :
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="1.php">
<p>
<input type="submit" name="Submit" value="Current-stat">
</p>
</form>
</body>
</html

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="2.php">
<p>
<input type="submit" name="Submit" value="Maintenance-Env">
</p>
</form>
</body>
</html

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="3.php">
<p>
<input type="submit" name="Submit" value="Prodcution-Env">
</p>
</form>
</body>
</html

Php script is actually calling a load balancer to change the group priority

1.php

cat  1.php
<?php
$command1 = '/usr/bin/sudo /usr/bin/ssh root@192.168.0.1 b pool test1 list';
#$rt = shell_exec(escapeshellcmd($command));
$rt1 = shell_exec($command1);
echo $rt1;
?>


cat 2.php
<?php
$command1 = '/usr/bin/sudo /usr/bin/ssh root@192.168.0.1 bigpipe pool test1 { \
      lb method member ratio \
      min active members 1 \
      member 192.168.10.150:80 ratio 4 priority 2 \
      member 192.168.10.151:80 ratio 2 priority 2 \
      member 192.168.10.152:80 ratio 10 priority 10 }
';
$command2 = '/usr/bin/sudo /usr/bin/ssh root@192.168.0.1 b pool test1 list';

#$rt = shell_exec(escapeshellcmd($command));
$rt1 = shell_exec($command1);
$rt2 = shell_exec($command2);
echo $rt1;
echo $rt2;
?>

cat 3.php
<?php
$command1 = '/usr/bin/sudo /usr/bin/ssh root@192.168.0.1 bigpipe pool test1 { \
      lb method member ratio \
      min active members 2 \
      member 192.168.10.150:80 ratio 4 priority 2 \
      member 192.168.10.151:80 ratio 2 priority 2 \
      member 192.168.10.152:80 ratio 1 priority 1 }
';
#$rt = shell_exec(escapeshellcmd($command));
$command2 = '/usr/bin/sudo /usr/bin/ssh root@192.168.0.1 b pool test1 list';

#$rt = shell_exec(escapeshellcmd($command));
$rt1 = shell_exec($command1);
$rt2 = shell_exec($command2);
echo $rt1;
echo $rt2;
?>


0
 
Mark BradyConnect With a Mentor Principal Data EngineerCommented:
Just to explain the (window.location.href = ) above. The reason you should add 'href' to the location is so older browsers will understand it. Some browsers will do nothing or just hang if you omit the .href part. Or you can omit the 'window' part and just have:

<input type="button1" onclick="location.href = 'path/1.php'"/>
0
 
Mark BradyConnect With a Mentor Principal Data EngineerCommented:
Firstly, the error you get is because you have a # hash before the $ variable. Remove it and it should run.
Your biggest problem is your form. You have open and closed the HTML 3 times in the one page?
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="1.php">
<p>
<input type="submit" name="Submit" value="Current-stat">
</p>
</form>
</body>
</html          and this is missing a closing tag '>'

My suggestion would be to write either 1 HTML page and have it submit it's forms to 1 php page. For eg:

create 3 different forms in your html page but DON't close the </html> tag until the end of the page. You don't need to use javascript to submit the form either. Just using action="1.php" is good enough. Your php script is very messy. Perhaps if you explain what it is you are trying to achieve in detail we can help you but your code will not work as it is.
0
 
AnanthahkAuthor Commented:
Hello Thansks for pointing fix on HTML code, My php script is actually logiing to device, executing a command and displaying the same.

I will try to clean up the script

Thanks all for your time

Regards,
Anantha
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.