Jump to content

inserting a custom form into wordpress


lindstrom1989

Recommended Posts

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-->

Link to comment
Share on other sites

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 by lindstrom1989
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.