get_user_meta and update_user_meta on the same page - php

I have a little form that updates a custom user meta I made named "Doel":
<?php $user_ID = get_current_user_id(); $user_data = get_user_meta($user_ID); ?>
<form method="POST" action="" id="doel_edit">
<input type="text" value="<?php if(isset($user_data['doel'][0])): echo $user_data['doel'][0]; endif; ?>" name="doel">
<button value="done" type="submit" form="doel_edit">Opslaan</button>
</form>
When I submit the form I get the standard update_user_meta() function to run
<?php if(isset($_POST['doel'])) {
update_user_meta($user_ID, 'doel', $_POST['doel']);
} ?>
No this all works fine, but when the page loads after submitting the form it echo's the old value from before update_user_meta() ran. I have to reload the page by hand to get my new value.
Why doesn't the value update directly? How can I fix that?
I think running functions like these in the wp-admin works fine.

I actually found the answer with help from somewhere else.
Because I ran get_user_meta() before update_user_meta() the values didn't update. Once I placed update_user_meta() after get_user_meta() it worked:
<?php $user_ID = get_current_user_id(); ?>
<?php if(isset($_POST['doel'])) {
update_user_meta($user_ID, 'doel', $_POST['doel']);
} ?>
<?php $user_data = get_user_meta($user_ID); ?>
<form method="POST" action="" id="doel_edit">
<input type="text" value="<?php if(isset($user_data['doel'][0])): echo $user_data['doel'][0]; endif; ?>" name="doel">
<button value="done" type="submit" form="doel_edit">Opslaan</button
</form>

Related

How to exclude/remove an input after the php code is executed.?

So i have an post input where i submit data something simple.
<form method="post" action="result.php">
<input type="url" name="url" class="form-control" placeholder="http://example.com/">
<input type="submit" name="submit" />
</form>
After the html code is going to be executed a php code which echo success or something like this that doesn't matter.
But i have a problem when i include('submit.php') it's going to show also the input and i don't want this.
How i can do that to don't show the input on result.php?
If you want it to be user-specific, you can try to use cookies or sessions like this:
index.php
<?php
session_start();
?>
<?php if(!isset($_SESSION['show_button']) && !$_SESSION['show_button'] ){ ?>
<!-- Button logic here... -->
<?php } ?>
result.php
// If the url has been entered, it returns a false from empty()
$_SESSION['show_button'] = empty($_POST['url']);

How to keep values in re-opened form?

I have two PHP files: index.php with a form, and data.php for further data manipulation.
Here is index.php:
<?php
session_start();
require_once("../index.conf");
$language = new Language();
$lang = $language->getLanguage(#$_POST['lang']);
?>
...
<form name="myForm" action="data.php" method="post" onsubmit="return validateForm()">
<input type="text" name="title" placeholder="e.g.: my_title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>">
...
<button class="btn_r" name="submit" type="submit">
<?php echo $lang['submit-button']; ?>
</button>
Here is data.php:
// success message
echo sprintf('
<div class="success">Good job! Your file <em>'.$file.'</em> was successfully created with this HTML content:<br>
<form name="goto_preview" method="post">
<input type="hidden" name="img_title" value="'.$title.'">
<button class="btn_l" name="reset" type="submit" name="logout" formaction="preview.php">PREVIEW RESULTS</button>
<button class="btn_r" name="submit" type="submit" name="continue" formaction="index.php">CORRECT DATA</button>
</form>
</div>',$img_name);
I try to return the user to the form, with the original values filled in if correction is needed. But the form always opens empty. What is wrong with my code?
Nothing is wrong with your code. That's just the way php forms work, you're redirecting to a new page therefore the form isn't filled out. To change that you could pass the POST arguments that you receive in the data.php and pass them back to index.php where you set them as default values (if present)

How can i fetch and display the value from external page to parent page's html element

Here is my first.php and second.php
Where i am just submitting the form itself, but i am including the second.php file which has the function get() which wil return 'ok'.
So I am calling the get() function after the form is posted.
Now how can I get the values and display it in the text fetchedvalue ?
first.php
<?php
include 'second.php';
?>
<form method="post" action="">
<input type="text" name="fetchedvalue">
<input type="submit" name="login" value="Submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
get();
}
?>
second.php
<?php
function get()
{
echo 'ok';
}
Note :
I have many data like that, How can I do it in a single form submit and fill all such textbox ?
try using this method:
<?php
include 'second.php';
$fvalue = '';
if(isset($_POST['fetchedvalue']))
$fvalue = $_POST['fetchedvalue'];
?>
<form method="post" action="">
<input type="text" name="fetchedvalue" value="<?=$fvalue;?>">
<input type="submit" name="login" value="Submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
get();
}
?>
notice that $fvalue is the variable responsible for creating value in the "fetchedvalue" input tag.. if the post is still not done, then the value would be "".
$_POST['fetchedvalue'] will contain it.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login']))
{
echo "<pre>";
print_r($_POST); // Here you will get all posted values & then you can call get method
echo "</pre>";
get();
}
?>
You can access the variables by using $_POST like an array, so:
$fetchedValue = $_POST['fetchedvalue'];
Though, a safer approach would be the following:
<?php
include 'second.php';
$fetchedValue = filter_input(INPUT_POST, 'fetchedvalue', FILTER_SANITIZE_STRING);
?>
<form method="post" action="">
<input type="text" name="fetchedvalue" value="<?php echo $fetchedValue; ?>">
<input type="submit" name="login" value="Submit">
</form>
filter_input() will return null if no POST variable with the given name is defined, and if it is, it will apply some sanitation or validation on it. Have a look at the manual for more detailed information:
http://php.net/filter_input

Execute A Function from Functions.php in Form

I'm building a form which displays a certain piece of user_meta data and I want to be able to update this usermeta on form submit.
This is what I have at the moment;
function my_form_funnction() {
global $current_user;
$vat_no = get_user_meta( get_current_user_id(), 'vat_number', true );
?>
<form name="setPrices" action="" method="POST">
<label for="lowPrice">Vat Number: </label>
<input type="text" id="vat_number" name="vat_number" value="<?php echo $vat_no ?>" />
<button type="submit">Save</button>
</form>
<?php update_user_meta( get_current_user_id(), 'vat_number', esc_attr($_POST['vat_number']) );
}
But the thing is the php that updates the user meta does so when the page loads, I only want it to do it when the user pressess save.
You need to use Ajax to update information on a page without refreshing it. Try jQuery! It is a realy simple way to use Ajax. For example:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<button onclick="javascript:la()">Save or do smth</button>
<div id="a">Here will be included the loadable thing</div>
<script>
function la(){
$("#a").load("/functions.php?func=my_form_function");
}
</script>
And in functions.php you may set:
if(isset($_GET["func"]){
if($_GET["func"] == "my_form_function"){
my_form_funnction();
}
}

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>

Categories