I know that the correct way to insert the data is using AJAX, but I don't mind if the page refreshes. Can anybody help me clear the form data after the page refreshes? Everything works fine, the data is being submitted to the table, and the page refreshes. But if I hit the refresh button again, it tells me that the data will be submitted ... and I don't know what to do.
<?php
$current_user = wp_get_current_user();
if ( $current_user->ID == 0 ) {
} else {
if( isset( $_POST['drop_artists'] ) ) {
$answer = $_POST['drop_artists'];
}else{
$answer = $_POST['artist_name'];
}
$date = current_time( 'mysql' );
$table = "t4t5_answers";
$sql = $wpdb->prepare( "INSERT INTO $table (user_id, post_id, info, answer, submission_date ) VALUES ( %d, %d, %s, %s, %d )", $current_user->ID, $post->ID, 'artist', $answer, $date );
$wpdb->query($sql);
header('Location: ' . get_bloginfo('url'));
}//if(isset($_POST['form_sub']))
?>
<form method="post" action="" id="artists-form">
<ul>
<li id="categories">
<?php
$args = array(
'show_option_all' => 'Artists',
'hierarchical' => 1,
'child_of' => 406,
'order_by' => 'name',
'name' => 'answer',
'hide_empty' => 0
);
wp_dropdown_categories($args); ?>
</li>
</ul>
<input type="text" name="artist_name" value="" size="45" id="input-title"/>
<input id="submitButton" class="subput" type="submit" name="submit" value="Add"/>
</form>
After the page is posted, use a redirect to send te user to a success page. This will make the page that was actually posted not appear in their history.
if (!empty($_POST)) {
// do stuff
header("Location: $_SERVER[PHP_SELF]");
}
Note the URL for the redirect should be a full URL, not a relative one. In practice though, I haven't seen any browsers have a problem with the relative URL.
To redirect to your current url immediately, try this one
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$current_URL.'">'
for more details see http://en.wikipedia.org/wiki/Meta_refresh
Write your php code inside if(isset($_POST['submit']) and add following script below insert statement.
echo " <script type='text/javascript'>
window.location=document.location.href;
</script>";
Your code will look like the following:
global $wpdb,$post;
if(isset($_POST['submit']))
{
$current_user = wp_get_current_user();
if ( $current_user->ID == 0 )
{
}
else
{
if( isset( $_POST['drop_artists'] ) )
{
$answer = $_POST['drop_artists'];
}
else
{
$answer = $_POST['artist_name'];
}
$date = current_time( 'mysql' );
$table = "t4t5_answers";
$sql = $wpdb->prepare( "INSERT INTO $table (user_id, post_id, info, answer, submission_date ) VALUES ( %d, %d, %s, %s, %d )", $current_user->ID, $post->ID, 'artist', $answer, $date );
$wpdb->query($sql);
}
echo "<script type='text/javascript'>
window.location=document.location.href;
</script>";
}
?>
<form method="post" action="" id="artists-form">
<ul>
<li id="categories">
<?php
$args = array(
'show_option_all' => 'Artists',
'hierarchical' => 1,
'child_of' => 406,
'order_by' => 'name',
'name' => 'answer',
'hide_empty' => 0
);
wp_dropdown_categories($args); ?>
</li>
</ul>
<input type="text" name="artist_name" value="" size="45" id="input-title"/>
<input id="submitButton" class="subput" type="submit" name="submit" value="Add"/>
</form>
This will refresh your page after form submit and form values will not be resubmitted.
Related
<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.
I create a code to save data from Form to custom_post_type.
Everything ok on Firefox but when I test it on edge, the post still save, but I can't edit the post on admin, or view the post(object not found) (see the image)
I really don't know where is the problem, please help.
this is my code, i copy it from my function.php:
function wpshout_frontend_post() {
wpshout_save_post_if_submitted();
?>
<div>
<div>
RESERVATION
</div>
<form id="new_post" class = 'datcho' name="new_post" method="post">
<input type = 'text' name = 'ten' required>
<input type = 'tel' name = 'sdt' required>
<input type = 'number' name = 'num' min='1' max = '10' required>
<input type = 'date' name = 'ngay' required>
<input type = 'time' name = 'time' value = '13:00' min='9:00' max='21:00' required>
<?php wp_nonce_field( 'wps-frontend-post' ); ?>
<input type="submit" value="Reservation" id="submit" name="submit"/>
</div>
</form>
</div>
<?php
}
function wpshout_save_post_if_submitted() {
// Stop running function if form wasn't submitted
if ( !isset($_POST['ten']) ) {
return;
}
// Check that the nonce was set and valid
if( !wp_verify_nonce($_POST['_wpnonce'], 'wps-frontend-post') ) {
echo 'Did not save because your form seemed to be invalid. Sorry';
return;
}
// Do some minor form validation to make sure there is content
// Add the content of the form to $post as an array
$title = wp_strip_all_tags($_POST['ten']);
$post = array(
'post_title' => $title,
'post_content' => $title,
'post_status' => 'Pending',
'post_type' => 'datcho'
);
$eror = wp_insert_post($post,true);
if($eror){
$sdt = wp_strip_all_tags($_POST['sdt']);
$ngay = wp_strip_all_tags($_POST['ngay']);
$time = wp_strip_all_tags($_POST['time']);
$num = wp_strip_all_tags($_POST['num']);
add_post_meta($eror, 'sdt', $sdt);
add_post_meta($eror, 'ngay', $ngay);
add_post_meta($eror, 'time', $time);
add_post_meta($eror, 'num', $num);
echo 'Saved your post successfully! :)';
}else {
echo "something wrong";
}
}
Here is the complete solution.
The issue mainly is you are not logged into the WP in Edge so when creating the post using wp_insert_post wordpress has no idea with which account should it attach that post.
function wpshout_frontend_post() {
wpshout_save_post_if_submitted();
?>
<div>
<div>
RESERVATION
</div>
<form id="new_post" class = 'datcho' name="new_post" method="post">
<input type = 'text' name = 'ten' required>
<input type = 'tel' name = 'sdt' required>
<input type = 'number' name = 'num' min='1' max = '10' required>
<input type = 'date' name = 'ngay' required>
<input type = 'time' name = 'time' value = '13:00' min='9:00' max='21:00' required>
<?php wp_nonce_field( 'wps-frontend-post' ); ?>
<input type="submit" value="Reservation" id="submit" name="submit"/>
</form>
</div>
<?php
}
function wpshout_save_post_if_submitted() {
// Stop running function if form wasn't submitted
if ( !isset($_POST['ten']) ) {
return;
}
// Check that the nonce was set and valid
if( !wp_verify_nonce($_POST['_wpnonce'], 'wps-frontend-post') ) {
echo 'Did not save because your form seemed to be invalid. Sorry';
return;
}
// Do some minor form validation to make sure there is content
// Add the content of the form to $post as an array
$title = wp_strip_all_tags($_POST['ten']);
$author_id = 1; // You can change it with your User ID
$post = array(
'post_title' => $title,
'post_content' => $title,
'post_status' => 'draft',
'post_type' => 'datcho'
'post_author' => $author_id
);
$eror = wp_insert_post($post,true);
if($eror){
$sdt = wp_strip_all_tags($_POST['sdt']);
$ngay = wp_strip_all_tags($_POST['ngay']);
$time = wp_strip_all_tags($_POST['time']);
$num = wp_strip_all_tags($_POST['num']);
add_post_meta($eror, 'sdt', $sdt);
add_post_meta($eror, 'ngay', $ngay);
add_post_meta($eror, 'time', $time);
add_post_meta($eror, 'num', $num);
echo 'Saved your post successfully! :)';
}else {
echo "something wrong";
}
}
I am trying to build a wordpress plugin that gets info from my form and puts it in muy database. This works perfectly fine but whenever i try to redirecty the page to the page where you can edit the items i get an error that the headers are already send. I tried everything from putting the get_header(); below the redirect to trying to work with hooks buth nothing works. There must be something that I am missing here.
<?php
global $wpdb;
$path='admin.php?page=my_pirazzo_locations';
$path2='admin.php?page=my_pirazzo_items';
$url=admin_url($path);
$url2=admin_url($path2);
//global $wpdb;
$userdb = $wpdb->prefix. 'users';
$getdata = $wpdb->get_results("SELECT * FROM $userdb");
//if (isset($_GET['id']))
//{
// $id= $_GET['id'];
// echo $id;
//}
//echo $userdb;
if (isset($_POST["submit"]) && $_POST["client"] != "" && $_POST["startdate"] != "" && $_POST["enddate"] != "") {
global $wpdb;
$userdb = $wpdb->prefix . 'users';
$table = $wpdb->prefix . "b2bdomain";
$user_id = strip_tags($_POST["client"]);
$startdate = strip_tags($_POST["startdate"]);
$enddate = strip_tags($_POST["enddate"]);
$isActive = 0;
if ($startdate < $enddate) {
$isActive = 1;
}
$query = $wpdb->get_results("SELECT user_login FROM $userdb WHERE ID = $user_id");
$name = $query[0]->user_login;
$wpdb->insert(
$table,
array(
'userid' => $user_id,
'isActivated' => $isActive,
'name' => $name,
'start_time' => $startdate,
'end_time' => $enddate,
)
);
$url = 'admin.php?page=my_pirazzo_edit';
ob_clean();
ob_start();
wp_redirect($url);
};
?>
<!-- This file should primarily consist of HTML with a little bit of PHP. -->
<div class="wrap">
<h1>beheer zone</h1>
<div class="postbox">
<div class="meta-th">
<h2>nieuwe zone toevoegen</h2>
</div>
<div class="meta-td">
<form method="post" name="cleanup_options" action="">
<fieldset>
<label for="client">Selecteer een Klant</label>
<select name="client" id="client">
<?php foreach($getdata as $data){ ?>
<option value="<?php echo $data->ID ?>"><?php echo $data->user_login ?></option>
<?php } ?>
</select>
</fieldset>
<fieldset>
<label for="startdate">Startdatum</label>
<input type="date" name="startdate" >
<label for="enddate">Einddatum</label>
<input type="date" name="enddate" >
</fieldset>
<?php submit_button('bewaar de zone', 'primary','submit', TRUE); ?>
</form>
</div>
</div>
<hr>
<div class="postbox">
<h2>voeg locatie's toe</h2>
<button class="button-primary">voeg een locatie toe</button>
</div>
<hr>
<div class="postbox">
<h2>voeg Producten toe</h2>
<button class="button-primary">voeg een locatie toe</button>
</div>
You can try sending your post data in wp_loaded hook. You can only use wp_redirect before header content is sent to the browser. So, instead of processing form in the template file. Moving them into wp_loaded hook might help you.
Please check below function for reference. Refer for https://cdn.tutsplus.com/wp/authors/legacy/tom/2012/09/25/wordpress-core-load-lifecycle.png
add_action( 'wp_loaded', 'pirazzo_edit_form' );
function pirazzo_edit_form(){
if( isset($_POST["submit"]) && $_POST["client"] != "" && $_POST["startdate"] != "" && $_POST["enddate"] != "") {
//YOUr additional code here
// process form, and then Redirect
$url = 'admin.php?page=my_pirazzo_edit';
wp_redirect($url);
exit();
}
}
Put the above code in your functions.php file to test it out.
Also, are you new to WordPress ? There are few things that hit me when i read your code properly. Now, there are many functions that comes built in with WordPress core for example querying the users. I see you have used wpdb object which you don't need to do.
This below is for your user selection field. Following is how we can query users directly using get_users function. Refer https://codex.wordpress.org/Function_Reference/get_users
<select name="client" id="client">
<?php $all_users = get_users(); ?>
<?php foreach($all_users as $data){ ?>
<option value="<?php echo $data->ID ?>"><?php echo $data->first_name .' ' .$data->last_name; ?></option>
<?php } ?>
</select>
Similarly, i do not see any nonce fields in your form submission. Please use nonce for security. Please check following on how we can implement this in WordPress.
<form method="post" name="cleanup_options" action="">
<?php
//Please check why this is needed https://codex.wordpress.org/Function_Reference/wp_nonce_field
wp_nonce_field( '_nonce_demo_action_field', '_demo_action_name' );
?>
Add the above code wp_nonce_field() below form or anywhere inside form. You are posting nonce secret data. So, we will be validating this when we are intercepting the post request sent like in the following:
//Functions.php
add_action( 'wp_loaded', 'pirazzo_edit_form' );
function pirazzo_edit_form(){
if( isset($_POST["submit"]) && wp_verify_nonce( $_POST['_demo_action_name'], '_nonce_demo_action_field' ) ) {
global $wpdb;
$userdb = $wpdb->prefix . 'users';
$table = $wpdb->prefix . "b2bdomain";
$user_id = strip_tags($_POST["client"]);
$startdate = strip_tags($_POST["startdate"]);
$enddate = strip_tags($_POST["enddate"]);
$isActive = 0;
if ($startdate < $enddate) {
$isActive = 1;
}
$query = $wpdb->get_results("SELECT user_login FROM $userdb WHERE ID = $user_id");
$name = $query[0]->user_login;
$wpdb->insert(
$table,
array(
'userid' => $user_id,
'isActivated' => $isActive,
'name' => $name,
'start_time' => $startdate,
'end_time' => $enddate,
)
);
$url = 'admin.php?page=my_pirazzo_edit';
wp_redirect( $url );
exit;
}
}
This above should be added in functions.php file. I have not touched anything inside the code. So, you will need to refactor it yourself.
Hope you understand the process i have written. Let me know on the comments if there are any confusions.
I'm trying to create a plugin with a visual editor and add media button that can POST and insert into the database all the HTML data. Here is my code:
<?php
if($_POST){
print_r($_POST);
global $wpdb;
$table_name = $wpdb->prefix . "eventi_ecm";
$name = $_POST['name'];
$text = $_POST['content'];
//$rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'name' => $name, 'text' => $text ) );
}
$settings = array('textarea_name' => 'content','media_buttons' => true,'tinymce' => false);
?>
<div>
<h2>New Event</h2>
<form method="post" action="http://test.ble-group.com/wordpress/wp-admin/admin.php?page=eventi_new_page">
<div id="poststuff">
<input type="text" name="name"/>
<?php wp_editor( '', 'content', $settings ); ?>
</div>
</div>
<input type="hidden" name="action" value="update" />
<p><input type="submit"/></p>
</form>
</div>
But when I try to insert images, the HTML code about the image it's stripped. :(
I found this article which might help you.
In short, article suggests to use this code:
<?php wp_editor( stripslashes($content), $editor-id ); ?>
and this code to display the output:
<?php echo wpautop(stripslashes($editor-id)); ?>
thanks #user850010, but didn't worked, I solved this way:
echo '<pre>' . htmlspecialchars( stripslashes($_POST['content']) ) . '</pre>';
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.