in wordpress get post methods does not work - php

I have made a site in Wordpress.
In my front-page.php and in my index.php files I entered both post and get methods just to echo a message in page-message.php file.
When I click the subscribe button the posts page:news appears.The posts page:news is defined in my Wordpress settings: dashboard=>settings=>reading=>Front page displays=>a static page.
In page-message.php I wanted to show the data I had entered from this form. T
Code for the forms:
<h2>Sign to Newsletter</h2>
<form action="page-message.php" method="post">
Name:<input class="input" type="text" name="name" value=""/><br/>
Email:<input type="text" name="email" value=""/>
<br/><input class="submit" type="submit" value="Subscribe"/>
</form>
Code for page-message.php:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
echo 'Congratulations!You have been successfully subscribed to our newsletter' .$name .$email;
?>
When I click the subscribe button my url changes to http://localhost/wordpress/page-message.php?name=stergios&email=something%40someone.com.
This seems to be correct when using the post method. The problem is the content!
It always loads the news (posts page from settings =>reading) and not the simple echo message?
It always shows the posts page when I write anything beyond my pages in the url. For example the url localhost/wordpress/hhhhhhhhhhh (a page that does not exist) does load the posts page and not a message error!

if "page-message.php" is a custom template in your theme you can get the page ID where it is assigned to with following query:
$get_message_pageId = new WP_Query(array(
'post_type' => 'page',
'meta_key' => '_wp_page_template',
'meta_value' => 'page-message.php'
));
and then you can set the action with:
action="<?php echo esc_url( get_permalink( $get_message_pageId->posts[0]->ID ) ); ?>"
so you will always have the correct URL in your form, but it will only work if you have 1 page where this template is assigned to

I know the reason why it is not working.
action="page-message.php"
your static page's path should be /wp-content/themes/{themename}/page-message.php u can try it,
action="<?php echo get_template_directory(); ?>/page-message.php"
But I don't think it will work. You can try
action="/index.php"
It is untested. let me know if it work.
Update: As you set that page as front page, then your wordpress root url will be that page's url (example.com or example.com/wpdirectory). Then simply do it,
action="/"
or
action="/wpdirectory"

Related

Custom result page upon submitting search form

I'm running in circle, i'm trying to run a query from a custom search form and to display the result on a specific custom page.
I've created a custom search page called search-custom.php and i've registered it.
Now I don't understand why when I submit my seachform i'm getting a 404 error.
This is my searchform currently, upon submitting the form, the url path seems right, am I missing something?
<form action="<?php echo esc_url( home_url().'/search-custom' ); ?>" method="get">
<input type="hidden" name="s" value="<?php the_search_query(); ?>">
<input type="hidden" value="qcm" name="post_type">
<button type="submit"><span class="mr-2">Go</button>
</form>
I don't want to redirect it from the function.php file I need this current format. Thanks in advance for your lights.
You do not need to change the action of your form to the name of your template file. Inside of your search-custom.php there is:
<form action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
<input type="hidden" name="s" value="<?php the_search_query(); ?>">
<input type="hidden" value="qcm" name="post_type">
<button type="submit"><span class="mr-2">Go</button>
</form>
With this template file having set, you can use it in your page template (the page you want the form to display, or maybe in a sidebar or something) with:
<?php get_template_part( 'search', 'custom' ); ?>
The default wordpress search template will be used to output the results.
If you want to use a different template file for the results, you can put this in the functions.php file of your theme and change the results page (i.e. search-results-custom.php):
add_action('template_include', 'search_custom_template');
function search_custom_template( $template ) {
if ( isset( $_REQUEST['search'] ) && is_search() ) {
$temp = locate_template('search-results-custom.php');
if ( ! empty($temp) ) {
$template = $temp;
}
}
return $template;
}
Multiple search results pages using different forms
If you want to set a value in your form, and using that to get to specific result pages:
Open the search.php file of your theme, make a copy for safety and replace the whole content of the file with the following code:
<?php
if(isset($_GET['post_type'])) {
$type = $_GET['post_type'];
if($type == 'qcm') {
load_template(TEMPLATEPATH . '/search-custom.php');
} elseif($type == 'another') {
load_template(TEMPLATEPATH . '/search-another.php');
}
}
?>
In this code we check what value is set for the field with the name post_type. In your question, you have set the value of qcm. When the field has this value on submit, you will load the search-custom.php. If you have other forms with other values, you can just add an elseif and load another search results template.
If you want to use another hidden input for checking the value, you can simply create one:
<input type="hidden" name="my-hidden-info" value="value-to-check" />
As you see, this way you can edit a value inside of your form and get different search results pages for the specific values. The action of the form is not changed and you will always use the search.php file. You just add other templates, which you are loading inside of this file in the if and elseif. So you get multiple search results pages using different forms.

error with submitting a form in wordpress

i've created a plugin (simple form) and this is the shortcode.php file:
<?php
add_shortcode('contact_form','contact_form');
function contact_form(){
if (isset($_POST['submit'])){
global $wpdb, $table_prefix;
$name = $_POST['name'];
$data = array('name'=>$name,'message'=>'message');
$wpdb->insert($table_prefix.'contact_form',$data,array('%s','%s'));
echo 'added';
}
?>
<form method="POST" action="" id="contact_form">
Name: <input type="text" name="name"><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
when I submit the form, if I write sth in the text field, is says 'The page doesn't exist' but if i leave it empty, it submits the form.
what is the problem?
I have used this shortcode in a page.
See this question on the WordPress StackExchange site. In summary, WordPress gets confused by a field called name. Try renaming it to (say) contact_name.
Loads more detail here (also wpse).

Product details not showing as intended

I found this php project on github. Vanilla Kit [https://github.com/syndicatefx/vanilla-kit] , is a very simple php powered dynamic website template, I liked the clean folder structure and hence decided to use/try it.
This is the index page on the root directory which is pretty much simple. It calls the page requested by the user (in the folder pages) and replaces the variable $page with page name requested for and displays it.
<?php
// Defualt page will always be pages/homepage.html, if not, change this to the name of the file you have created to be the homepage.
$page = 'homepage';
// Get pages based on user input
if (!empty($_GET['name'])) {
//Assign a variable to a sanitised version of the data passed in the URL
$tmp_page = basename($_GET['name']);
//If the file exists, update $page
if (file_exists("pages/{$tmp_page}.php"))
$page = $tmp_page;
//If the file does not exist, include notfound page and exit
elseif(!file_exists($tmp_page)){
include 'pages/notfound.php';
exit;
}
}
// Include $page (declared default)
include ("pages/$page.php");
?>
The default page which is the homepage fetches products from the products table and displays it.
<?php
// Edit this page's title, description and keywords for SEO
$pagetitle = 'Welcome';
$pagedescription = 'description goes here...';
$pagekeywords = 'keywords,go,here';
// Add a class to body for more CSS power
$bodyclass = 'home';
// Do Not Remove
include 'inc/header.php';
?>
<?php
$dbquery = "SELECT * FROM lbtbl_products";
$productresult = $dbconnect->query($dbquery);
if ($productresult->num_rows > 0) {
while($row = $productresult->fetch_assoc()) { ?>
<div class="prod-cnt prod-box">
<form method="post" action="cartupdate.php">
<h3 class="prod-title">
<?php echo $row["lbproductName"]; ?>
</h3>
<p><?php echo $row["lbproductDescription"];?></p>
<div class="price-cnt">
<div class="prod-price"><img src="images/common/rupees.png" width="7" height="10"/> <?php echo $row["lbproductPrice"];?></div>
Qty <input type="text" name="product_qty" value="1" size="3" />
<button class="add_to_cart">Add To Cart</button>
<input type="hidden" name="product_code" value="<?php echo $row["lbproductSku"];?>" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="<?php echo $current_url;?>" />
</div>
</form>
</div>
<?php }
} else {
echo "0 results";
}
$dbconnect->close(); ?>
<?php include 'inc/footer.php'; ?>
Everything works fine up till now.
Now, to display the product details I have a page name productdetails.php which is saved in the pages directory as all other pages. The link on the 'homepage' to view the product detail is
<?php echo $row["lbproductName"]; ?>
But once clicked a 404 not found page is displayed. But if I move the productdetails.php page to the root directory it works. Can anyone help/suggest with a solution. My best guess it has something to do with index.php code after the comment // Get pages based on user input.
Try and share the results.
<?php echo $row["lbproductName"]; ?>
And in your product detail page you should run your select query with $_GET['id'].
It may work, try and share the results.

Stay on same page after form submit within WordPress

I've added my HTML page as part of the admin wordpress (see screenshot)
And I'm trying to get it so on each submit button, if the record is successfully added to the database a pop up shows up saying "Success!" and then clear the form data without leaving the page. At the moment my current set up tries to load the external page instead of remaining on the page in the screenshot. Here's my HTML and PHP:
<form class="pure-form" name="addAthlete" action="submitForm.php" method="POST">
<fieldset class="pure-group">
<input type="text" class="pure-input-2" name="membershipID" placeholder="Membership ID">
<input type="text" class="pure-input-2" required="required" name="firstName" placeholder="First Name">
<input type="text" class="pure-input-2" required="required" name="surname" placeholder="Surname">
</fieldset>
<button name="submitAthlete" type="submit" class="pure-button pure-input-2 pure-button-primary">Submit</button>
</form>
<?php
function showSuccess() {
echo "Success! One record added.";
}
if (isset($_POST['submitAthlete'])) {
addAthlete();
}
function addAthlete() {
include 'addAthlete.php';
showSuccess();
}
?>
I'm assuming the problem lies with the fact that the echo "Success" is trying to echo on the submitForm.php page which is how I've written it. This is because of the way the page is embedded into wordpress, you can see this below:
add_action( 'admin_menu', 'db_menu' );
function db_menu() {
add_menu_page(
'Database Form', // page title
'Database Form', // menu title
'manage_options', // capability
'database-form', // menu slug
'wpse_91693_render' // callback function
);
}
function wpse_91693_render() {
global $title;
print '<div class="wrap">';
print "<h1>$title</h1>";
$file = plugin_dir_path( __FILE__ ) . "submitform.php";
if ( file_exists( $file ) )
require $file;
print '</div>';
}
How can I get a pop up or something to show up within this WordPress page on each submit?
Thanks!
When you submit a form, it posts data to a new webpage by loading it. In order to stay on the same page, the action should take you to that same page ( move your processing logic there aswell, checking for posted arguments ) or if you don't want to see a reload, use ajax instead.

I want to convert a php/mysql application to a wordpress plugin

I have a php/mysql web application.I want to convert it in a wordpress plugin.How can I make link of one php file to another file.
For example I have plugin tw. tw.php is its index file and the other file name is tw1.php.
first page
/* plugin name:tw
plugin url:httpL://csr.estheticsolutions.com.pk
*/
//tell wordpress to register the demolistposts shortcode
add_shortcode("demo-list-posts", "demolistposts_handler");
function demolistposts_handler()
{
//run function that actually does the work of the plugin
$demolph_output = my_function();
//send back text to replace shortcode in post
return $demolph_output;
}
function my_function()
{
?>
<html>
<body>
<form action="tw1.php" method="post">
<input type="text" name="fname" >
<input type="submit" value="Submit" >
</form>
</body>
</html>
<?php
}
?>
second page
<?php
$First_Name=$_POST['fname'];
echo $First_Name;
?>
when i submit value in tw.php ,an error appears tw1.php not found(404) in wordpress folder,then i paste the file tw1.php in that(wordpress) folder and submit, posted value get in new page without wordpress theme.
Create a table with name=people with columns: id, firstname, lastname, DateofBirth, addressid, fatherid, motherid. Have suitable column properties with each column.
You can use the plugin url function to link to the second file:
<form action="<?php echo plugins_url( 'tw1.php', dirname(__FILE__) ); ?>" method="post">
<input type="text" name="fname" >
<input type="submit" value="Submit" >
</form>

Categories