lindstrom1989 Posted August 8, 2014 Share Posted August 8, 2014 Hi, I have built a custom php program for a client which takes information submitted in a html form and calculates a persons bmi. I have tested and it works fine, this is in one page (one file). My problem is I have tried to insert it in to wordpress using both shortcodes and php tag plugins (you can't insert php in to posts and pages as standard on wordpress) and I can't get it to work. The closest I've got is using a php tag plugin that uses shortcodes to replace <?php & ?> but when I submit, it just reloads the page and form rather than showing the result. as I've said it works fine outside of wordpress. <h1>BMI Calculator</h1> <!--bmi form--> <?php if (!isset($_POST['submit'])){ ?> <form action="<?php echo $_SERVER['PHP_SHELF']; ?> "method="post"> <input name="weight" size="3"> Enter your weight in kilograms (KG). </br> </br> <input name="height" size="3"> Enter your height in metres, for example if you're 160cm tall you would type in "1.60". </br> </br> <input type="submit" name="submit" value="Calculate"> </form> <?php } else { $weight = $_POST ['weight']; $height = $_POST ['height']; $bmi_catagory = array("Underweight", "Normal", "Overweight", "Obese"); $bmi = ($weight/$height)/$height; if ($bmi <= 18.5) { echo '<h2>Your bmi is '.round($bmi).'</h2><p>You are '.$bmi_catagory[0] .' Click <a href=\'../bmichecker.php\'>HERE</a> to see how Hypnoslimtize can help you achieve a healthy weight.</p>'; } elseif (($bmi > 18.5) && ($bmi <=24.9)) { echo '<h2>Your bmi is '.round($bmi).'</h2><p>You are '.$bmi_catagory[1] .' Click <a href=\'../bmichecker.php\'>HERE</a> to see how Hypnoslimtize can help you stay healthy and avoid temptations.</p>'; } elseif (($bmi >= 25) && ($bmi <=29.9)) { echo '<h2>Your bmi is '.round($bmi).'</h2><p> You are '.$bmi_catagory[2] .' Click <a href=\'../bmichecker.php\'>HERE<a/> to see how Hypnoslimtize can Help you achieve a healthy body weight.</p>'; } elseif ($bmi >= 30) { echo '<h2>Your bmi is '.round($bmi).'</h2><p> You are '.$bmi_catagory[3] .' Click <a href=\'../bmichecker.php\'>HERE</a> to see how Hypnoslimtize can help you achieve a healthy body weight.</p>'; } else { echo 'Your bmi is '.round($bmi); } } ?> <!--bmi form end--> Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 8, 2014 Share Posted August 8, 2014 Look at the action of your form open tag ... "PHP_SHELF" should be PHP_SELF? Quote Link to comment Share on other sites More sharing options...
lindstrom1989 Posted August 9, 2014 Author Share Posted August 9, 2014 Are you asking me a question or telling me the code needs a question mark at the end? :s Quote Link to comment Share on other sites More sharing options...
lindstrom1989 Posted August 9, 2014 Author Share Posted August 9, 2014 (edited) Oh sorry I see what you mean, I tried that and now it redirects to the website index.php instead of refreshing the current page. I'd doe the same with the code in the index.php It's supposed to just run the script when submit is hit not load pages :s Could this be because of the way wordpress generates it's pages? Edited August 9, 2014 by lindstrom1989 Quote Link to comment Share on other sites More sharing options...
lindstrom1989 Posted August 9, 2014 Author Share Posted August 9, 2014 I've done some digging around and replaced PHP_SELF with REQUEST_URI which reloaded the same page the form is in instead of running the script... Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 9, 2014 Share Posted August 9, 2014 You can also totally leave off the action to have it submit to the current url. Like: <form method="post"> Quote Link to comment Share on other sites More sharing options...
lindstrom1989 Posted August 9, 2014 Author Share Posted August 9, 2014 Yeah that does the same thing. The problem is it's refreshing my page and form rather than submitting data and running the script in the page. The form works fine in my local server mamp but I get this problem when trying to add to wordpress :s Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 9, 2014 Share Posted August 9, 2014 Sorry, I don't know enough about custom wordpress coding to help further. Quote Link to comment Share on other sites More sharing options...
lindstrom1989 Posted August 9, 2014 Author Share Posted August 9, 2014 Thanks for your help I have figured it out, I wasn't doing anything wrong in the end. I tried two plugins to allow me add php scripts into wordpress pages. I Just tried a 3rd one and it works... if anybody else has the same problem try exec-php plugin to allow php tags in pages and posts. This has however bought along a newer problem although i assume it's a little simpler... the form is at the bottom of the page and when submitted it refreshes to the top of the page, is there a way to create some kind of anchor for it on the page to push down to on the refresh but only once the form has been submitted? Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 9, 2014 Share Posted August 9, 2014 Hmm haven't tried it with wordpress, but you could give your form an ID and in the form action submit with that #. like <form id="the-id" action="<?php echo $_SERVER['REQUEST_URI']; ?>#the-id"> Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 9, 2014 Share Posted August 9, 2014 Oh that won't work for what you're doing because the form doesn't display. Do the same thing above except instead of giving the form an ID give the <h2> tags the id. Quote Link to comment Share on other sites More sharing options...
lindstrom1989 Posted August 9, 2014 Author Share Posted August 9, 2014 Fantastic! thank you VERY much! The only thing I would like to try and do with this is to eventually turn this in to a plugin so that people can use shortcodes to add this form anywhere they like on their own wordpress sites, if anybody has any words of wisdom or good links to read up on i would be grateful. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.