Site logo

Archived topic

Calling a PHP Function in a Form

8 replies · Started by culpable on December 26, 2018

Viewing posts 1–9 of 9

Hi,

I am trying to convert a page that was entirely coded in PHP into a page on a conventional "wordpress page".

To do this I have added a HTML block on a wordpress page with all of my HTML code.

I have used simple CSS to add the CSS specific to my page.

Now the PHP – my code calls the PHP function in a form via:

<form action="submitvoltage.php" method="post" id="myForm3">

Where can I put this "submitvoltage.php" function so that it will be correctly called?

Can I add the code via an Elements hook that only displays on this page? e.g. -> https://i.imgur.com/Mw935GN.png

Thank you in advance for your help :)

You should be able to add that to Elements, but you might need to adjust the action so it has the full URL to submitvoltage.php.

Sorry for the late reply Tom.

After a lot of troubleshooting, I'm still a bit stuck.

Using "submitresistance.php" as an example, this is the code right now:

$voltage=0;
$voltageunit ="";
$current=0;
$currentunit ="";
$resistance=0;
$resistanceunit="";
$power=0;
$powerunit="";
if((!empty($_POST['voltage1'])) && (!empty($_POST['current1']))){
$voltage = $_POST['voltage1'];
$voltageunit = $_POST['voltageunit1']; 
if($voltageunit =="volt"){$voltage=1*$voltage;}else{if($voltageunit =="mili volt"){$voltage=0.001*$voltage;}else{if($voltageunit =="kilo volt"){$voltage=1000*$voltage;}}}
$current = $_POST['current1'];
$currentunit = $_POST['currentunit1'];
if($currentunit =="ampere"){$current=1*$current;}else{if($currentunit =="mili ampere"){$current=0.001*$current;}else{if($currentunit =="kilo ampere"){$current=1000*$current;}}}
$resistance=$voltage/$current;
if($resistance>999999){$resistance=$resistance/1000000;$resistanceunit="M&ohm;";}else{if($resistance>999){$resistance=$resistance/1000;$resistanceunit="K&ohm;";}else{$resistance=$resistance/1;$resistanceunit="&ohm;";}}
$power=$voltage*$current;
if($power>999999){$power=$power/1000000;$powerunit="MW";}else{if($power>999){$power=$power/1000;$powerunit="KW";}else{$power=$power/1;$powerunit="W";}}
echo number_format((float)$resistance, 4, '.', '');
echo $resistanceunit;
echo number_format((float)$power, 4, '.', '');
echo $powerunit;
}else{echo "<h3 class='errormessage'>Please enter non zero voltage and current value</h3>";}

No need to get into the details... but essentially I'm thinking that how can my code know where to find and call this "submitresistance.php" if it is not named as a file when it is on the page via "Elements -> Hooks -> Execute PHP".

Would this require me converting the above into a PHP function, and then submitting the form to that function?

Sorry for my ignorance on the matter. I'm just trying to extrapolate from other programming languages I've worked in.

That file would need to exist somewhere on your server - it can't be added using a hook.

You can add it to a child theme, and then link your form action to the file.

Thanks for your help Tom.

I made a child theme and added the file in the same folder as my child theme's function.php file: https://i.imgur.com/XhVzxog.png

I also changed the "submit" to add a "/" to take it from the root: https://i.imgur.com/AHDxQ3E.png

But from the above error, I presume that I am pointing at the wrong place?

Hmmm so I added the above PHP code in full to a 'submitresistance.php' file which I added to my root directory.

<?php
$voltage=0;
$voltageunit ="";
$current=0;
$currentunit ="";
$resistance=0;
$resistanceunit="";
$power=0;
$powerunit="";

if((!empty($_POST['voltage1'])) && (!empty($_POST['current1']))){
$voltage = $_POST['voltage1'];
$voltageunit = $_POST['voltageunit1']; 
if($voltageunit =="volt"){$voltage=1*$voltage;}else{if($voltageunit =="mili volt"){$voltage=0.001*$voltage;}else{if($voltageunit =="kilo volt"){$voltage=1000*$voltage;}}}
$current = $_POST['current1'];
$currentunit = $_POST['currentunit1'];
if($currentunit =="ampere"){$current=1*$current;}else{if($currentunit =="mili ampere"){$current=0.001*$current;}else{if($currentunit =="kilo ampere"){$current=1000*$current;}}}
$resistance=$voltage/$current;
if($resistance>999999){$resistance=$resistance/1000000;$resistanceunit="M&ohm;";}else{if($resistance>999){$resistance=$resistance/1000;$resistanceunit="K&ohm;";}else{$resistance=$resistance/1;$resistanceunit="&ohm;";}}
$power=$voltage*$current;
if($power>999999){$power=$power/1000000;$powerunit="MW";}else{if($power>999){$power=$power/1000;$powerunit="KW";}else{$power=$power/1;$powerunit="W";}}

?>
<div>Resestance<div><div><?php echo number_format((float)$resistance, 4, '.', '');?></div><div><?php echo $resistanceunit;?></div></div></div>

<div>Power<div><div><?php echo number_format((float)$power, 4, '.', '');?></div><div><?php echo $powerunit;?></div></div></div>
<?php
}else{echo "<h3 class='errormessage'>Please enter non zero voltage and current value</h3>";}
?>

I then altered the form to point at the root.... i.e. "/submitresistance.php".

It is no longer throwing a "not found" error. But it looks like it’s evaluating the "else" part of the statement above and is just showing the <h3> part.

Then is also carrying across onto the page itself when I hit "calculate", as seen here: https://imgur.com/a/BpnI4il

I'm stumped as to why this works on a non-wordpress install (calling the script "submitresistance.php") but in this case, it's not working. Aren't the situations analogous?

If you have any ideas Tom, they would be very much appreciated.

Since the file isn't in the root directory (you could move it there, though), you'd need to add the full URL to the file: yoursite.com/wp-content/themes/generatepress_child/yourfile.php

You're too great Tom.

Working! Thank you :)

You're welcome :)

This archived topic is closed to new replies.