Why textarea is showing old content on form submition? - php

I have a textarea and I can update its content, then I send it to db when I click a submit button:
<form action="" method="POST" class="myForm" autocomplete="off">
<textarea name="newContent" class="form-control" rows="10"><?php the_content(); ?></textarea>
<input id="update_content" name="subContent" type="submit" value="Update">
</form>
<?php
$post = array(
'ID' => $id,
'post_content' => $_POST['newContent']
);
if ('Update' === ($_POST['subContent'] ?? false)) {
wp_update_post($post, true);
}
?>
It all works absolutely fine, db is updated. Yet as the page reloads on form submit, the textarea shows the old content. If I manually refresh the page, then I see the new content.
I've also tried adding <meta http-equiv="Cache-control" content="no-cache">

Related

Why Wordpress wp_update_post function removes html form tags?

I'm trying to add a form with hidden inputs into the post content using external PHP fle.
When i try it from the browser the form added successfully but when i try it from command line, the form inputs are deleted.
Here is my code:
require('../wp-load.php');
$content = '<div class="buy-preowned">
<form accept-charset="UTF-8" action="https://www.tesla.com//order?redirect=no" id="tesla-cpo-marketing-buy-form" method="post">
<div>
<input name="CPOvehicle" type="text" value="1"/>
<input name="VIN" type="hidden" value="5YJSA1E1XHF210809"/>
<input name="vehicleMapId" type="hidden" value="1280359"/>
<input name="titleStatus" type="hidden" value="NEW"/>
<input name="form_id" type="hidden" value="tesla_cpo_marketing_buy_form"/>
</div>
</form>
<p class="small-text">
Requires a $2,500 deposit
</p>
</div>
';
$post_id = 1;
$data = array(
'ID' => $post_id,
'post_content' => $content,
);
When i checked the database, the form is stored as:
<form accept-charset="UTF-8" action="https://www.tesla.com//order?redirect=no" id="tesla-cpo-marketing-buy-form" method="post">
<div>
</div>
</form>
this is only happen when i rub the script from command line, any idea how to resolve it.
Thank you
It s because of built-in security filters. As it is not normal to store some form data inside content(there are shortcodes for that), WP disabled inserting some non-text tags.
But anyway, you can enable it manually.
kses_remove_filters();
$post_id = 1;
$data = array(
'ID' => $post_id,
'post_content' => $content,
);
wp_update_post($data);
kses_init_filters();

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.

how to show results from script in php on client side (html)

I have a quick form in HTML , where the user should write a number.I should provide this number to script in PHP using method GET.Results from this scrip should be shown on client side ( page in html with form) in JSON format. I do not know how to do this.
My code :
<?php
$data=
array(
'12345678912'=>array('name'=>'Insurance Company A' , 'number' => '123'),
'98765432109'=>array('name'=>'Insurance Company B' , 'number' => '312'),
'80101066666'=>array('name'=>'Insurance Company B' , 'number' => '980'),
);
if ($data[$_GET['pesel']]) {
echo json_encode($data[$_GET['pesel']]);
}
else {
echo 'wrong number';
}?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1250">
</head>
<body>
<form action="server.php" method="get">
<div style="border:solid; width:500 px; height:300 px;">
<h1> Write number </h1>
<input type="text" name="pesel" />
<input type="submit" value="ok" />
<div>
<div id="results">
//place to show result in JSON
</div>
</form>
</body>
</html>
Result on server's page for number 12345678912
{"name":"Insurance Company A","number":"123"}
How can show it in div id=results on page ?
Sorry for my english...
it is only one php file which is calling itself again.
input.php:
<form action="input.php" method="get">
<input type="number" name="number" />
<input type="submit" value="submit"/>
</form>
<?php
$num = $_GET['number'];
echo $num
?>
i dont know why you want to show it in browser in json format, json format is rather used for transfer purposes and not for displaying.
So you want to display json_encode($data[$_GET['pesel']]); in #results? Just put the code echoing the stuff there instead:
<div id="results">
<?php
if ($data[$_GET['pesel']]) {
echo json_encode($data[$_GET['pesel']]);
}
else {
echo 'wrong number';
?>
</div>
And change the form so that it submits to the same URL you're displaying it on,
<form action="" method="get">
...
</form>
When submitted, the browser will call the same URL with the GET parameter from the form. This will execute your PHP code, outputting the results.
But it's a weird thing you're doing...

Page reload ask to submit data over and over again

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.

Submit button to affect multiple files

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 }

Categories