Submit button to affect multiple files - php

Current Situation:
So, for an article page (article.php), I have 2 different sections (title, content), which are hooked to the article page from their own php files (Title is pulled from title.php while content is pulled from content.php).
Now, I added an ability for the author to edit the title and content on the article which they need to press submit button in order save the change.
Problem
After editing, the author needs to press "Submit" button in order to update the data.
However, when I put the "submit" button in either title.php or content.php, only the corresponding section is updated while the other is unchanged (for example, if I have this submit button in the title.php then only title is edited).
If I put the button in the article.php then of course neither the title nor content gets updated.
Here is the mark up:
Article.php:
<div class="article">
<?php do_action ('article_summary'); ?>
</div>
Title.php (with the submit button, which only updates the title)
<form role="form" method="post">
<div class="edit_title">
<input type="hidden" value="<?php echo $post_id; ?>">
<?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
<div class="update-button-wrap">
<input type="submit" name="update_product" value="<?php esc_attr_e( 'Update', 'site' ); ?>"/>
</div>
</div>
</form>
Content.php
<form role="form" method="post">
<div class="edit_content">
<?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
</div>
</form>
Does anyone know how the submit button can save both title and content that are in two different files?
Thanks bunch!

Try using a super-button (pattent pending) that presses all them other buttons for ye...
<button type='button' onclick="$('[type="submit"]').click()">Submit</button>
Since its type button, it won't submit itself. Replace each submit button with this. I mean, leave the original buttons there, but style them with
button[type="submit"] { display:none }

Related

Front-end button that automatically adds a tag to the post - WORDPRESS

I've been researching this all day and haven't found an elegant solution, what I'm trying to do is add a 'featured' tag to a post when a button is clicked and also removing the 'featured' tag when another button is clicked.
For Instance;
When the Mark Featured button is clicked, the post will have the Featured tag
When the Unmark Featured button is clicked, the Featured tag is removed.
Here is my try
<form name="front_end_featured" id="FeaturedTag" method="POST" action="/dashboard" >
<fieldset>
<input type="hidden" name="Featured" id="FEATURED_BTN" value="featured" />
<?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
<button class="button" id="featured" type="submit"><?php _e('Mark as Featured', 'framework') ?></button>
</fieldset>
</form>
<?php
// If a user is logged in, and a form has been submitted with new tags
if ( is_user_logged_in() && isset( $_POST['Featured'] ) ) {
// Add the tags
wp_set_post_tags(get_the_ID(), sanitize_text_field($_POST['Featured']), true );
} ?>

How to know if 'input submit button' is clicked (wordpress)?

I need to know if 'input submit button' is clicked.
I tried following php codes, and clicked a save_progress button, but it echos 'EMPTY'.
Would you please let me know how to know it the button is clicked or get the value of the button?
Existing code (submit page):
<form class="acfef-form -submit" action="" method="post" autocomplete="disableacf" novalidate="novalidate" data-widget="fc8926d" id="acf-form-fc7226d18999" enctype="multipart/form-data">
<div class="1"></div>
<div class="2"></div>
<div class="3">
<div class="3-1"><input class="acfef-submit-button" type="submit" value="Save" data-state="publish"></div>
</div>
<div class="4"><input class="save-progress-button" name="save_progress" type="submit" data-state="revision" value="Revision"></div>
</form>
php codes I tried in the submit page:
if(isset($_POST['save_progress'])) {
echo 'NOT EMPTY ';
}else{
echo 'EMPTY ';
}
if (empty($_POST['save_progress'])){
if( array_key_exists( 'save_progress', $_POST ) ){
Thank you.
Specify method="post" like
<form class="acfef-form" method="post">
Also you should place your PHP code into a WP Hook, like init.

How to change the posts category with a button click

I am trying to create a button "Approved" to change the post category from it's current one to the "approved" category. I don't mind if it reloads the page or not. I would also like to redirect the page afterward to the next post in the original category.
I have found some questions on this already but am ultimately lost on how to get this all together and working.
<?php add_shortcode('approved_button', 'brist_approved_button_function');
function brist_approved_button_function() {
ob_start(); ?>
<form method="post" action="approved.php">
<input type="submit" value="Approved" name="submit"> <!-- assign a name for the button -->
</form>
<?php
wp_set_object_terms( $post_id, intval( $_POST['approved'] ), 'category', false );
$output = ob_get_clean();
return $output;
}?>
I figured it out. Though it was a head-scratcher for me. If anyone else if having the same issue, note the code below. You have to create the select and then have the 'selected' option on the category you want the post to change to. Then, in the CSS, hide the select input, only leaving the button.
<?php add_shortcode('approved_button', 'approved_button_function');?>
<?php function approved_button_function() { ob_start();?>
<div class="approval">
<form action="" id="update-post" method="post">
<?php wp_dropdown_categories( "selected='categoryId'&exclude=21&class=approval-select&show_count=1&hierarchical=1&orderby=name&order=ASC&&hide_empty=0&show_option_all=Choose An Option" ); ?>
<input class="approval-button" type="submit" name="submit" value="Approve" />
</form>
</div>
<?php if ( array_key_exists('cat', $_POST )) {
global $post;
$post_id = $post->ID;
wp_set_object_terms( $post_id, intval( $_POST['cat'] ), 'category', false );
} ?>

Updating different forms at the same time

Current Situation
So, this is a follow up question from my previous question (Submit button to affect multiple files)
The approach was good in terms of how to "click" multiple buttons together by using a "super-button" (patent pending) by #oMiKeY. It does what it is supposed to do, clicking all buttons.
The mark up is the following:
Title.php
<form role="form" method="post">
<div class="edit_title">
<input type="hidden" value="<?php echo $post_id; ?>">
<?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
<div class="update-button-wrap">
<input id="save_button" type="submit" name="update_product" value="<?php esc_attr_e( 'Update', 'site' ); ?>"/>
</div>
</div>
</form>
Content.php
<form role="form" method="post">
<div class="edit_content">
<?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
</div>
<div class="update-button-wrap">
<input id="save_button" type="submit" name="update_product" value="<?php esc_attr_e( 'Update', 'site' ); ?>"/>
</div>
</form>
Article.php
<button type='button' onclick="$('[type="submit"]').click()">Submit</button>
Problem
So, when the "Submit" button is clicked, both buttons in each title.php and content.php are also clicked (ie. in regards to clicking buttons, it works fine). However, because two forms are simultaneously clicked, only the second one is updated (either content or title) while the first one is ignored, when both data are needed to be updated.
My approach
Now, I can merge two files together and have both title and content within a single form, but that really messes up my overall setup and I heard it is better to have multiple smaller php files for updating and speed than a large one big file.
Or here is my another approach.
In the article.php, I will have the form and submit button while the title.php and content.php only has the editable forms. Then these two forms are somehow linked to the form in article.php, like the image below.
Do you think the second approach can be achieved or any other suggestions?
Thanks
Just add a global form on article.php and drop the title and content forms (and submit buttons). Every named input inside the global form will be submitted together no matter what php file generated them.
Edit:
Title.php
<div class="edit_title">
<input type="hidden" value="<?php echo $post_id; ?>">
<?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
</div>
Content.php
<div class="edit_content">
<?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
</div>
Article.php
<form role="form" method="post">
<?php include "Title.php"; include "Content.php"; ?>
<button type='submit'>Submit</button>
</form>
Option 2:
Alternatively you could use some sort of functionality that allows you to create forms only once, even if you have "inner forms". This way Title.php and Content.php would also work as standalone code snippets.
$formDeep = 0;
function openForm() {
global $formDeep;
if ($formDeep == 0) {
echo "<form role=\"form\" method=\"post\">";
}
$formDeep++;
}
function closeForm() {
global $formDeep;
$formDeep--;
if ($formDeep == 0) {
echo "</form>";
}
}
Title.php
<?php openForm(); ?>
<div class="edit_title">
<input type="hidden" value="<?php echo $post_id; ?>">
<?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
</div>
<?php closeForm(); ?>
Content.php
<?php openForm(); ?>
<div class="edit_content">
<?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
</div>
<?php closeForm(); ?>
Article.php
<?php openForm(); ?>
<?php include "Title.php"; include "Content.php"; ?>
<button type='submit'>Submit</button>
<?php closeForm(); ?>

get action to happen, only when submit button is pressed

i have some code that will post to the users wall, however, at the minute it will post when the page is loaded, i need it to post only when the 'post to my wall button is submitted.
here is my code:
<div align="center">
<form method="GET" action="translate.php">
<textarea name="status2" cols="50" rows="5"<input type="text"/>
<?php echo str_ireplace(array ('old','awkward','all','again','behind','along','alright','hello','among','children','yes','child','kids','food','barnard castle','beer','book','blow','beautiful','bird','burst','brown','burn','boots'),
array ('auld', 'aakwad', 'aall','agyen','ahint','alang','alreet','alreet','amang','bairns','aye','bairn','bairns','bait','barney','beor','beuk','blaa','bonny','bord','borst','broon','bourn','byeuts'),$status); ?>
</textarea><br>
<input type="submit" value="post to wall"
// i did try my wall code here but it still posted on page load
/>
</form>
</div>
<?php
$args = array(
'message' => 'Hello World',
'link' => 'http://apps.facebook.com/geordie-status/',
'caption' => 'Translate from English to Geordie'
);
$post_id = $facebook->api("/$uid/feed", "post", $args);
?>
Add name attribute to your input tag. and use isset for check if the user pressed the submit button.
<input type="submit" value="post to wall" name="submit"
// i did try my wall code here but it still posted on page load
/>
</form>
</div>
<?php
if (isset($_POST['submit'])){
$args = array(
'message' => 'Hello World',
'link' => 'http://apps.facebook.com/geordie-status/',
'caption' => 'Translate from English to Geordie'
);
$post_id = $facebook->api("/$uid/feed", "post", $args);
}
?>
You should put the code for the posting to the wall inside translate.php since that is the page listed in the form action. When the form gets submitted, values will be passed as parameters to translate.php then you can use $_GET to fetch them and execute the code that writes to the wall.
I think your problem is that your browser re-sends data each time the page is reloaded.
There are 2 approaches:
Redirect user to the same page, so sent data will be cleared (header("Location: asd"))
Store some hash in session, make a hidden input and check whether the hash is right. Change the hash when the form is correctly submitted.

Categories