Page reload ask to submit data over and over again - php

I have the following *code below, every time I refresh the page it asks me to send the form again. How do I avoid that, I don't want to resend a form on a page refresh. Thanks in advance.
*CODE
<?php function make_user_feedback_form() {
global $wpdb;
global $current_user;
$ufUserID = $current_user->ID;
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'updateFeedback' ) {
$ufDataUpdate = $wpdb->insert( 'wp_user_feedback', array( 'date' => current_time('mysql'), 'responses' => $_POST["test"]) );
}
}?>
<div id="form">
<ol>
<form method="post">
<li><label for="test">Question 01</label><input type="text" id="datepicker" name="test" value="" /></li> <!-- the (name="test") value is what the ('responses' => $_POST["test"]) value is talking too -->
<li><input name="submit" type="submit" id="submit" class="submit button" value="Send feedback" /></li>
<?php wp_nonce_field( 'updateFeedback' ); ?>
<input name="action" type="hidden" id="action" value="updateFeedback" />
</form>
</ol>
</div>
<?php
add_action('the_content','make_user_feedback_form');
?>

After you have processed the form data and stored it in the database or worked with it in some way, reload the same page using:
header("location: thispage.php");
Doing this will destroy the POST data and allow the page to be refreshed without displaying the resubmit alert.

Related

Page not loading when trying to handle forms

I'm new to PHP and I'm trying to handle a form so I added a if condition to check if the submit button was pressed and if the other elements in the form are set. For some reason it causes the page to not load (it loads, but shows a blank page) so I can't actually submit anything..
When I tried removing the php code, it loaded fine. What am I doing wrong here?
<body>
<?php
if(isset(filter_input(INPUT_POST, 'aglogin')) && isset(filter_input(INPUT_POST, 'agname')) && isset(filter_input(INPUT_POST, 'agpass')))
{
echo 'Submitted..';
}
?>
<form method="post">
Username: <input type="text" id="agname" name="agname"/>
<br>
Password: <input type="password" id="agpass" name="agpass"/>
<br>
<input type="submit" id="aglogin" name="aglogin" value="Login">
</form>
</body>
Do not use isset(); because filter_input() returns true if variable is set and return false if it is not set.
<body>
<?php
if(filter_input(INPUT_POST, 'aglogin') && filter_input(INPUT_POST, 'agname') && filter_input(INPUT_POST, 'agpass'))
{
echo 'Submitted..';
}
?>
<form method="post">
Username: <input type="text" id="agname" name="agname"/>
<br>
Password: <input type="password" id="agpass" name="agpass"/>
<br>
<input type="submit" id="aglogin" name="aglogin" value="Login">
</form>
You could try this
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$valid=( array_key_exists('aglogin',$_POST ) && array_key_exists('agname',$_POST ) && array_key_exists('agpass',$_POST ) ) ? true : false;
if( $valid ) echo "Form submitted successfully";
}
?>

Form button to redirect user to specific page

I have a function on a page which takes a user to another page if the action is equal to 'add'. I now want to create a button which will do this for me without typing &action=add into the url.
The function looks like this:
function consultants_page() {
if (isset($_GET['action']) && $_GET['action'] == 'add') {
require( get_template_directory() . '/settings/pages/consultant_add.php' );
}else {
require( get_template_directory() . '/settings/pages/consultant.php');
}
}
I then need to amend this button to link to this function, but it isn't currently working..
<form method="get" action="add">
<p class="submit"><input type="submit" name="add" id="submit" class="button button-primary" value="Add"></p>
</form>
cheers
Something like this will send the variable "action" to the url:
<form method="get" >
<p class="submit"><input type="submit" name="action" id="submit" class="button button-primary" value="add"></p>
</form>
with your html you where sending add?add=Add.
Your php code should get the variable with $_GET["action"].
use $_POST instead of $_GET, that way the values will not appear in the url.
function consultants_page() {
if (isset($_POST['action']) && $_POST['action'] == 'add') {
require( get_template_directory() . '/settings/pages/consultant_add.php' );
}else {
require( get_template_directory() . '/settings/pages/consultant.php');
}
}
html
<form method="post" action="add">
<p class="submit"><input type="submit" name="add" id="submit" class="button button-primary" value="Add"></p>
</form>
don't forget to sanitize user input.

If the value exists show in wordpress admin (Form's have been created on the fly so number increments)

I'm creating my first options page and i'm trying to upload images for a slider, I don't know how many images are going to be in the slider so I will need to add more with a + button, It will automatically show one text input an upload button and a + button to start off with, should I require more i'll click the + button which will then add another text input, upload button a + button and a - button.
I've got this working to a point, it still needs a little help but it's getting there http://jsfiddle.net/vs8p5/5/
So far if I upload an image and click the + button, it will give me the option to upload another, this is working great and the images upload to wordpress.
Now for the issue.
I'm using this part of code to retrieve the data from wordpress
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
echo "<script type='text/javascript'>alert('yes it has');</script>";
}
This tells me that if there is a value then echo the alert
Now, working form that code above I've
function kandibox_hero_upload_image_link_callback($args) {
$hero_options = get_option( 'hero_options' ); ?>
<div id="upload_image_sets"> <?php
$hero_options = get_option ( 'hero_options' );
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) { ?>
<div id="clonedInput1" class="clonedInput">
<input id="upload_image_link_1" type="text" size="36" name="hero_options[upload_image_link_1]" value="<?php echo $hero_options['upload_image_link_1']; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}
else { ?>
<div id="clonedInput1" class="clonedInput">
<input id="upload_image_link_1" type="text" size="36" name="hero_options[upload_image_link_1]" value="<?php echo $hero_options['upload_image_link_1']; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div>
<?php } ?>
</div> <?php
}
What i'm trying to do here is echo a form with a value, if nothing exists, echo a blank form, this seems to work although I don't think it's correct.
The main issue is that it will only echo the first form, when you click the + buttons, the form's id, name, and value script increments.
So now the question.
How can I get the form to look for all the $hero_options['upload_image_link_...'] and echo out a form for each if it exists? then echo out 1 blank form if nothing exists?
I'm pretty sure i've covered everything you might need but if i've missed something, let me know and i'll add it.
I've followed about 20 tutorials old and new and come up with this.
To add the information to the wordpress database, i'm using the following code.
function register_hero_options() {
add_settings_section(
'hero_settings_section', // ID used to identify this section and with which to register options
__( 'hero Options', 'kandibox' ), // Title to be displayed on the administration page
'kandibox_hero_options_callback', // Callback used to render the description of the section
'hero_options' // Page on which to add this section of options
);
add_settings_field(
'show_hero_options', // ID used to identify the field throughout the theme
__( 'hero', 'kandibox' ), // The label to the left of the option interface element
'kandibox_toggle_hero_callback', // The name of the function responsible for rendering the option interface
'hero_options', // The page on which this option will be displayed
'hero_settings_section' // The name of the section to which this field belongs
);
$hero_options = get_option ( 'hero_options' );
if( isset( $hero_options['show_hero_options'] ) && $hero_options[ 'show_hero_options' ] ) {
add_settings_field(
'hero_size',
__( 'Size', 'kandibox' ),
'kandibox_hero_size_callback',
'hero_options',
'hero_settings_section'
);
add_settings_field(
'hero_background',
__( 'Background', 'kandibox' ),
'kandibox_hero_background_callback',
'hero_options',
'hero_settings_section'
);
add_settings_field(
'upload_image_links',
__( 'Upload Image', 'kandibox' ),
'kandibox_hero_upload_image_link_callback',
'hero_options',
'hero_settings_section'
);
}
register_setting(
'hero_options',
'hero_options'
);
}
add_action( 'admin_init', 'register_hero_options' );
$hero_options = get_option( 'hero_options' );
$count=count($hero_options);
$totalimg=$count-4; ?>
<div id="upload_image_sets"> <?php
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
for($i=1;$i<=$totalimg;$i++){ ?>
<div id="clonedInput1" class="clonedInput">
<input type="text" size="36" name="hero_options[upload_image_link_<?=$i ?>]" value="<?php echo $hero_options['upload_image_link_'.$i]; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}}
?>
ok i juss wrote a rough code for you might this will help you. this is according to the array you have go. there might be some better ways of doing it.
$hero_options = get_option( 'hero_options' );
$count=count($hero_options);
$totalimg=$count-4; ?>
<div id="upload_image_sets"> <?php
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
for($i=1;$i<=$totalimg;$i++){ ?>
<div id="clonedInput1" class="clonedInput">
<input type="text" size="36" name="hero_options[upload_image_link_<?=$i ?>]" value="<?php echo $hero_options['upload_image_link_'.$i]; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}}
?>
</div> <?php
Updated the code so now it's correct.

How to delete comments programmatically in WordPress?

I want my bloggers to be able to delete comments via the front end instead of the standard way via the WP dashboard. I wrote a function custom_delete_post_comment() which deletes a comment with a given ID.
function custom_delete_post_comment() {
$comment_id = comment_ID();
wp_delete_comment( $comment_id, true )
}
As you can see, my function uses WordPress' wp_delete_comment() function.
I plan on having a button next to each comment which when clicked will run the delete function I wrote hence removing the comment.
I have come up with a solution using the $_POST approach. My question is how do I modify my code so that the page reloads to reflect the fact that the comment has been deleted?
<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
set_query_var( 'commentid1', $_POST['commentid'] );
wp_delete_comment( get_query_var( 'commentid1'), true );
};
?>
<form class="delete-comment" action="" method="post">
<input type="hidden" name="commentid" value="<?php comment_ID() ?>" />
<input type="submit" value="Delete" title="Delete" class="btn" />
</form>

Problems logging out - have to click logout twice

I'm hoping someone can help with an issue which, intuitively, should be simple, but the answer eludes me. For some reason, when my user logs out, he has to click log out twice to make it work. I've read this:
Any idea why I have to click the logout button twice to logout?
But it doesn't seem to be working. Here is my logout button code. Any thoughts? Thank you!
<div id="loginStatusWrap">
<div id="loginStatus">
<?php
include('includes/APILogin.php');
if ( isset( $_POST['logout_btn'] ) )
{
unset($_COOKIE['kp_emailID']);
unset($_COOKIE['kp_pass']);
session_destroy();
}
// Check for login cookie - skip if session is available
if ( isset($_COOKIE['kp_emailID']) && isset($_COOKIE['kp_pass']) && !isset($_SESSION['kp_accountID']) )
{
$username = $_COOKIE['kp_emailID'];
$pass = $_COOKIE['kp_pass'];
$get_account_parameters = array(
'session' => $session_id,
'module_name' => 'kd_kp',
'query' => "kd_kp_cstm.username_c = '" . $_COOKIE['kp_emailID'] . "'",
'order_by' => "",
'offset' => '0',
'select_fields' => array(
//'username_c',
//'password_c',
//'id',
//'name',
),
'link_name_to_fields_array' => array( ),
'max_results' => '1',
'deleted' => '0',
'Favorites' => false,
);
$get_account_result = call('get_entry_list', $get_account_parameters, $url);
//echo '<pre>'; print_r($get_account_result); echo '</pre>';
if ( $_COOKIE['kp_pass'] != $get_account_result->entry_list[0]->name_value_list->password_c->value )
{
// not logged in
session_unset();
session_destroy();
?>
<div id="loginForm">
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="text" placeholder="EMAIL" value="<?=$_COOKIE['kp_emailID']?>" name="signIn_email" id="signIn_email"/>
<input type="password" placeholder="PASSWORD" id="signIn_pwd" name="signIn_pwd" />
<input type="submit" value="SIGN IN" name="login_btn" id="login_btn" />
<!--input type="submit" value="SIGN UP" name="signup_btn" id="signup_btn" /-->
</form>
</div><!-- //logInForm -->
<?php
} else {
// is logged in
$_SESSION['kp_accountID'] = $get_account_result->entry_list[0]->name_value_list->id->value;
$_SESSION['kp_name'] = $get_account_result->entry_list[0]->name_value_list->name->value;
// set cookies
$hour = time() + 3600;
setcookie("kp_emailID", $get_account_result->entry_list[0]->name_value_list->email1->value, $hour, "/", "kp.com");
setcookie("kp_pass", $get_account_result->entry_list[0]->name_value_list->password_c->value, $hour, "/", "kp.com");
?>
<div id="loginForm">
<h1>WELCOME, <?=$get_account_result->entry_list[0]->name_value_list->name->value?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="submit" value="LOG OUT" name="logout_btn" id="logout_btn" />
</form>
</div>
<?php
}
}
//if the login form is submitted
if ( isset( $_POST['login_btn'] ) )
{
// checks it against the database
if ( !get_magic_quotes_gpc() )
{
$_POST['signIn_email'] = addslashes($_POST['signIn_email']);
}
$get_account_parameters = array(
'session' => $session_id,
'module_name' => 'kd_kp',
'query' => "kd_kp_cstm.username_c = '".$_POST['signIn_email']."'",
'order_by' => "",
'offset' => '0',
'select_fields' => array(
//'username_c',
//'password_c',
//'id',
//'name',
),
'link_name_to_fields_array' => array( ),
'max_results' => '1',
'deleted' => '0',
'Favorites' => false,
);
$get_account_result = call('get_entry_list', $get_account_parameters, $url);
//Gives error if user dosen't exist
if ( $get_account_result->result_count == 0 )
{
?>
<div id="loginForm">
<a href='/sign-up.php'>Click Here to Register</a>
</div>
<?php
}
$_POST['pass'] = md5( stripslashes($_POST['signIn_pwd']) );
//gives error if the password is wrong
if ( $_POST['pass'] != stripslashes($get_account_result->entry_list[0]->name_value_list->password_c->value) )
{
//if ( !isset($get_account_result->entry_list[0]->name_value_list->password_c->value) )
{
?>
<div id="loginForm">
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="text" placeholder="EMAIL: TRY AGAIN" name="signIn_email" id="signIn_email"/>
<input type="password" placeholder="PASSWORD" id="signIn_pwd" name="signIn_pwd" />
<input type="submit" value="SIGN IN" name="login_btn" id="login_btn" />
<!--input type="submit" value="SIGN UP" name="signup_btn" id="signup_btn" /-->
</form>
</div><!-- //logInForm -->
<?php
}
//echo '<pre>'; print_r($_POST); echo '</pre>';
} else {
// if login is ok then we update session vars
$_SESSION['kp_emailID'] = stripslashes($_POST['signIn_email']);
//$_SESSION['kp_pass'] = $_POST['pass'];
$_SESSION['kp_accountID'] = $get_account_result->entry_list[0]->name_value_list->id->value;
$_SESSION['kp_name'] = $get_account_result->entry_list[0]->name_value_list->name->value;
?>
<div id="loginForm">
<h1>WELCOME, <?=$get_account_result->entry_list[0]->name_value_list->name->value?></h1>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="submit" value="LOG OUT" name="logout_btn" id="logout_btn" />
</form>
</div><!-- //logInForm -->
<?php
}
} else {
// check for active session
if ( isset($_SESSION['kp_accountID']) )
{
?>
<div id="loginForm">
<h1>WELCOME, <?=$_SESSION['kp_name']?></h1>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="submit" value="LOG OUT" name="logout_btn" id="logout_btn" />
</form>
</div>
<?php
} else {
// if they are not logged in
?>
<div id="loginForm">
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="text" placeholder="EMAIL" name="signIn_email" id="signIn_email"/>
<input type="password" placeholder="PASSWORD" id="signIn_pwd" name="signIn_pwd" />
<input type="submit" value="SIGN IN" name="login_btn" id="login_btn" />
<!--input type="submit" value="SIGN UP" name="signup_btn" id="signup_btn" /-->
</form>
</div><!-- //logInForm -->
<?php
}
}
//echo '<div style="float:left;"><pre>'; print_r($_POST); echo '</pre></div>';
//echo '<div style="float:left;"><pre>'; print_r($_SESSION); echo '</pre></div>';
//echo '<div style="float:left;"><pre>'; print_r($_COOKIE); echo '</pre></div>';
?>
<div class="clear"></div>
</div><!-- //loginStatus -->
</div><!-- //loginStatusBar -->
<!-- END loginbar.php !-->
Thank you!
Probably because this code isn't running before the part of the page that shows the user is logged in loads. Thus, the HTML shows that he is logged in but really he isn't.
The browser doesn't recognize cookie changes until after a page reload.
Your first click is unsetting the cookies, but the user remains logged in.
Your second click is "reloading" the page, where the cookies are now unset, and it appears to work.
Add a redirect to the same page (after unsetting and destroying the session) to simulate a refresh, and your button should work.
p.s.
This is a simplified version of how cookies work, but it's basically accurate.
Since you're unsetting the cookie on the server, you have to request the page again to be given the new http headers that DON'T contain the old cookie information. That's what the reload would do.
After you destroy the session, redirect to the login page, so that the page refreshes.
If you are logged into a third party website via FB, you must log out twice to complete the logout.

Categories