- This topic has 8 replies, 2 voices, and was last updated 7 years, 3 months ago by
Tom.
-
AuthorPosts
-
December 26, 2018 at 6:01 am #765727
culpable
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 🙂
December 26, 2018 at 9:17 am #765993Tom
Lead DeveloperLead DeveloperYou should be able to add that to Elements, but you might need to adjust the
actionso it has the full URL tosubmitvoltage.php.December 30, 2018 at 3:52 pm #769495culpable
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Ω";}else{if($resistance>999){$resistance=$resistance/1000;$resistanceunit="KΩ";}else{$resistance=$resistance/1;$resistanceunit="Ω";}} $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.
December 30, 2018 at 5:24 pm #769542Tom
Lead DeveloperLead DeveloperThat 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.
December 30, 2018 at 9:52 pm #769618culpable
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?
December 31, 2018 at 3:14 am #769695culpable
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Ω";}else{if($resistance>999){$resistance=$resistance/1000;$resistanceunit="KΩ";}else{$resistance=$resistance/1;$resistanceunit="Ω";}} $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.
December 31, 2018 at 8:30 am #769980Tom
Lead DeveloperLead DeveloperSince 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.phpJanuary 2, 2019 at 3:10 pm #771520culpable
You’re too great Tom.
Working! Thank you 🙂
January 2, 2019 at 3:31 pm #771527Tom
Lead DeveloperLead DeveloperYou’re welcome 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.