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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vrynxzentCommented:
inside index.php

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

<input type="button"> onclick="<?php THE SCRIPT FROM 1.php; ?>">
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shinesh PremrajanEngineering 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_PanchalCommented:
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
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Mark BradyPrincipal 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 BradyPrincipal 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 BradyPrincipal 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.