最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom PHP page form data is not appearing as a wp-theme page template

programmeradmin0浏览0评论

I have made an age calculator using PHP. It works fine in localhost/age/age-calculator.php

I have moved this file as a wp theme-page-template theme/mytheme/page-template/age-calculator.php

<?php get_header();
/*
Template Name: Age Calculator
*/

Create a page and linked with this page-template.

The input forms working nicely but when submit data. WordPress doesn't show me any result and bring me on the home page.

But in localhost, I am getting input result on the same page

How to show the result on the wp page template?

Thanks

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method='post'>
<div class="container bg-light p-2 wow bounceInUp">

    <div class="row">

        <div class="col-sm-6 mb-4">
    <p> Your Name * </p>
    <input type="text" name='person_1' placeholder="Name" <?php echo "$required $all_required";?> />
    <p class="text-danger"> <?php echo " $nameErr ";?> </p>
    <br/>

    <p> Date of Birth * </p>
    <input type="date" name='birth_date' <?php echo "$required $all_required";?> /> 
    <p class="text-danger"> <?php echo " $dateErr ";?> </p>
    <br/>

    <input  class="btn-success" type="submit" value='Calculate Yours Age' name='submit_1' /> <br/>

        </div>

        <div class="col-sm-6">
    <p> Your Friend's Name * </p>
    <input type="text" name='person_2' placeholder= "Friend's Name " <?php echo "$all_required";?> />
    <p class="text-danger"> <?php echo " $nameErr2 ";?> </p>
    <br />

    <p> Your Friend's Date of birth * </p>
    <input type="date" name='birth_date_2' <?php echo "$all_required";?> />
    <p class="text-danger">  <?php echo " $dateErr2 ";?> </p>
    <br /><br />

        </div>
    </div>
    <div class="row">
        <input class='m-auto btn btn-success p-2' type="submit" value='Compare Age' name='submit_all' /> 
    </div>
</div>
</form>

I have made an age calculator using PHP. It works fine in localhost/age/age-calculator.php

I have moved this file as a wp theme-page-template theme/mytheme/page-template/age-calculator.php

<?php get_header();
/*
Template Name: Age Calculator
*/

Create a page and linked with this page-template.

The input forms working nicely but when submit data. WordPress doesn't show me any result and bring me on the home page.

But in localhost, I am getting input result on the same page

How to show the result on the wp page template?

Thanks

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method='post'>
<div class="container bg-light p-2 wow bounceInUp">

    <div class="row">

        <div class="col-sm-6 mb-4">
    <p> Your Name * </p>
    <input type="text" name='person_1' placeholder="Name" <?php echo "$required $all_required";?> />
    <p class="text-danger"> <?php echo " $nameErr ";?> </p>
    <br/>

    <p> Date of Birth * </p>
    <input type="date" name='birth_date' <?php echo "$required $all_required";?> /> 
    <p class="text-danger"> <?php echo " $dateErr ";?> </p>
    <br/>

    <input  class="btn-success" type="submit" value='Calculate Yours Age' name='submit_1' /> <br/>

        </div>

        <div class="col-sm-6">
    <p> Your Friend's Name * </p>
    <input type="text" name='person_2' placeholder= "Friend's Name " <?php echo "$all_required";?> />
    <p class="text-danger"> <?php echo " $nameErr2 ";?> </p>
    <br />

    <p> Your Friend's Date of birth * </p>
    <input type="date" name='birth_date_2' <?php echo "$all_required";?> />
    <p class="text-danger">  <?php echo " $dateErr2 ";?> </p>
    <br /><br />

        </div>
    </div>
    <div class="row">
        <input class='m-auto btn btn-success p-2' type="submit" value='Compare Age' name='submit_all' /> 
    </div>
</div>
</form>
Share Improve this question edited Feb 12, 2020 at 16:32 Sheikh Sajib asked Feb 12, 2020 at 16:03 Sheikh SajibSheikh Sajib 112 bronze badges 4
  • What does your <form> tag look like? Does it have a method and an action? – WebElaine Commented Feb 12, 2020 at 16:25
  • <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method='post'> – Sheikh Sajib Commented Feb 12, 2020 at 16:29
  • To echo Elaine's comment, if you would add the code to your question, you are likely to get a more useful answer. – jdm2112 Commented Feb 12, 2020 at 16:29
  • 1 @jdm2112 thanks, I have added the form code only. Hole code is more then 400 line – Sheikh Sajib Commented Feb 12, 2020 at 16:35
Add a comment  | 

1 Answer 1

Reset to default 2

The approach I usually take is to check to see if your submit input exists and construct the page based on that condition.

For example, if your submit/send button was constructed similar to this:

<input class="cta" type="submit" name="submit_all" value="Send message">

Then your condition check would look like:

if ( isset( $_POST['submit_all'] ) ) {
    // Do the stuff with $_POST array
} else {
    // No submission yet, show the form
}

The key in the $_POST array will match the name attribute of your input.

Elaine's comment is a good one as the action attribute determines where to POST the submission data. In WordPress, using the permalink is a good option.

<form id="####" class="####" method="post" action="<?php echo esc_url( the_permalink() ); ?>">

EDIT: based on the code you added to the question, I recommend updating the action attribute on the form element. Also, I have updated the condition example to use your submit_all value.

发布评论

评论列表(0)

  1. 暂无评论