Why updating meta from front-end needs i get old value? - php

The new value is sent to the db and the page refreshes on submit, but the field
becomes empty. If I then manually refresh the page again, then I see
the new value. Any idea why?
<form action="" method="POST" class="myForm" autocomplete="off">
<input id="dateChange" type="text" name="input-test" value="<?php echo str_replace('_', '', $jikuDate); ?>" autocomplete="off">
<input id="test-form" type="submit" name="updateDate" value="Update">
</form>
and then I do
<?php
if(isset($_POST['updateDate'])){
$post = array(
'ID' => $id
);
if ('Update' === ($_POST['updateDate'] ?? false)) {
update_post_meta( $post->ID, 'usp-custom-14', $_POST['input-test']);
}
}
?>

This is how I resolved it, not the most elegant probably:
First:
<?php
$jikuDate = get_post_meta($post->ID, 'usp-custom-14', true);
if (isset($_POST['updateDate'])) {
// Execute this code if the submit button is pressed.
$jikuDate = $_POST['input-test'];
}
?>
Then the html:
<form action="" method="POST" class="myForm" autocomplete="off">
<input id="dateChange" type="text" name="input-test" value="<?php echo str_replace('_', '', $jikuDate); ?>" autocomplete="off">
<input id="test-form" type="submit" name="updateDate" value="Update">
</form>
Then:
<?php
if($_SERVER['REQUEST_METHOD']=="POST") {
if ('Update' === ($_POST['updateDate'] ?? false)) {
$post = array(
'ID' => $id
);
$jikuDate = $_POST['input-test'];
update_post_meta( $post['ID'], 'usp-custom-14', $_POST['input-test']);
}
}
?>

Related

Why does the validation for my codeigniter website fail everytime?

I have a controller code like so:
$validation=service('validation');
$validation->reset();
$data = [
'validation' => $validation,
];
if ($this->request->getMethod() === 'post'){
$validation->setRules( [ 'comment' => 'required',] );
if($validation->run()) {
$CommentsModel->save(['Comment' => $this->request->getPost('comment'),]);
return redirect()->to('/someplace...');
}
}
}
return view('templates/header', $data)
. view('templates/menu')
. view('addComment', $data)
. view('templates/footer');
}
Everytime my form is posted, the validation fails... even when I have something in comment! Any idea why this would be?
This is the form:
<form action="/Add/<?= esc($book)?>/<?= esc($chapter) ?>" method="post">
<?= csrf_field() ?>
<label for="comment">Comment</label>
<textarea name="comment" id="comment" ><?= set_value("comment")?></textarea><br />
<?php if($validation->hasError('comment')) echo '<p class="validationError">' . $validation->getError('comment') . '</p>'?>
<input type="submit" name="submit" value="Add Comment" />
</form>

Add action admin_post_nopriv_ not working in custom roles - wordpress

<form method="POST" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" enctype="multipart/form-data">
<input type="hidden" name="action" value="createsnew_form" />
<input type="text" name="newtitle" id="newtitle" placeholder="" />
<input type="submit" name="submit" id="submit" value="Create New" />
</form>
In my function.php
add_action( 'admin_post_nopriv_createsnew_form', 'addnewpostsform' );
add_action( 'admin_post_createsnew_form', 'addnewpostsform' );
function addnewpostsform()
{
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "createsnew_form" )
{
$post_type = 'custom_post_type';
$title = $_POST['newtitle'];
$new_post = array(
'post_title' => $title,
'post_status' => 'publish',
'post_type' => $post_type,
);
$pid = wp_insert_post($new_post);
wp_safe_redirect(esc_url(home_url('/')) . "thank-you/");
exit();
}
}
I create a custom role "Brand", when a user (role brand) login to the site and try to create a new post this code is not working. But a user is login with administrator privilege this code works. Is there any issue with my code.
When i submit the form it redirect to home page.

Can't access upload file input with php

I created a simple form, to create a post, that has three inputs:
One for the title
Description
Image
So, when I submit my form (using post) I call a php file, that "echoes" the value from each input.
It works just fine, but when I try to call the php function $_FILES['my_input_name']['tmp_name'], on my file input, I get an error saying:
Undefined index: my_input_name
My form looks like this (shorter version):
<form action="processForm.php" method="post">
<input type="text" name="title" class="input" required>
<textarea id="description" name="description"required></textarea>
<input type="file" name="fileMedia">
</form>
My php file looks like this
$method = $_SERVER[ 'REQUEST_METHOD' ];
if ( $method=='POST') {
$_args = $_POST;
$_INPUT_METHOD = INPUT_POST;
}
elseif ( $method=='GET' ) {
$_args = $_GET;
$_INPUT_METHOD = INPUT_GET;
}
else {
exit(-1);
}
$title = $_args['title'];
$description = $_args['description'];
$mediaName = $_args['fileMedia'];
$mediatmpPath = $_FILES["fileMedia"]["tmp_name"];
echo $title."<br>";
echo $description."<br>";
echo $mediaName."<br>";
echo $mediatmpPath ."<br>";
I have no idea of what I'm doing wrong, so any helped would be really apreciated!
P.s: My form's is really reduced. In the original one I have row, cols, divs, etc, and some other inputs, which I did not find relevant for this question
You just need to add multipart = "form/data" in form tag
You need to add this below line in <form> tag
<form action="processForm.php" method="post" enctype='multipart/form-data'>
<input type="text" name="title" class="input" required>
<textarea id="description" name="description"required></textarea>
<input type="file" name="fileMedia">
<input type="submit" name="save" value="save">
</form>
And below post data code:
<?php $method = $_SERVER[ 'REQUEST_METHOD' ];
if ( $method=='POST') {
$_args = $_POST;
$_INPUT_METHOD = INPUT_POST;
}
elseif ( $method=='GET' ) {
$_args = $_GET;
$_INPUT_METHOD = INPUT_GET;
}
else {
exit(-1);
}
$title = $_args['title'];
$description = $_args['description'];
$mediaName = $_FILES["fileMedia"]["name"];
$mediatmpPath = $_FILES["fileMedia"]["tmp_name"];
echo $title."<br>";
echo $description."<br>";
echo $mediaName."<br>";
echo $mediatmpPath ."<br>";
?>
I think this help you.

If statement in a form PHP

I'm working on a code which has a form. The answer on the form needs to decide to which page it should go, but it doesn't work.
<html>
<body>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
<?php
if($_GET["graden"]>=28){
$action = "Koelbox.php";
}
else{
$action = "scrabble.php";
};
?>
</form>
</body>
</html> ```
You have to declare and initialize the variable before using it.
<?php
if($_GET["graden"]>=28)
{
$action = "Koelbox.php";
}
else
{
$action = "scrabble.php";
}
?>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
</form>
First check the GET parameter, then set your $action variable accordingly and then use it in your echo.
I just initialized the variable before the form, but as far as I see the IF statement doesn't see the input in the form.
<?php
if($_GET["graden"]>=28)
{
$action = "Koelbox.php";
}
else
{
$action = "scrabble.php";
}
?>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
</form>

Publish a custom post type from front

I want to publish a custom post type 'question' from the front-end, but when I submit the form I keep getting 404 error. Bellow is the form and form processing. What am I doing wrong?
<?
/**
* Questions processing
*/
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please add a question';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please add a description';
}
// Add the content of the form to $post as an array
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_status' => 'publish',
'post_type' => 'question'
);
wp_insert_post($post); // Pass the value of $post to WordPress the insert function
} // end IF
// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post');
?>
<h1>Add a question:</h1>
<!-- New Question Form -->
<div>
<form name="new_post" method="post" action="">
<p><label for="title">Question:</label><br />
<input type="text" value="" name="title" />
</p>
<p><label for="description">Details</label><br />
<textarea name="description" cols="50" rows="6"></textarea>
</p>
<p><input type="submit" value="Ask!" name="submit" /></p>
<input type="hidden" name="post_type" value="question" />
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>
<!--// New Question Form -->
You don't actually need the add_action bit and I have a feeling is the $post variable itself that's causing issues.
/**
* Questions processing
*/
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please add a question';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please add a description';
}
// Add the content of the form to $post as an array
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_status' => 'publish',
'post_type' => 'question'
);
$id = wp_insert_post($new_post); // Pass the value of $post to WordPress the insert function
//Returns ID of the new post you just created
And just in case, also add a URL to your form tag:
<form name="new_post" method="post" action="<?php the_permalink(); ?>">
I figured out:
I've deleted the line:
<input type="hidden" name="post_type" value="question" />
I think Wordpress is using somehow a post variable with this name and it get's an error if I use it on my own.

Categories