get action to happen, only when submit button is pressed - php

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.

Related

Request form and POST in a single go?

Please excuse the poor title.
I am a total beginner and don't know the right terms to make it better.
I am trying to POST form data using PHP.
My problem is that before i POST the form data i need to get a value from the form, that is changing each time i request the page.
Please notice the second input, the value is auto generated and is a random number each time i request the form.
Here is my form.php:
<?php
if(!isset($_REQUEST['submit_btn'])){
echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<input type="text" name="user_name" id="user_name">
<input type="hidden" name="a_random_password" value="'.(rand(10,100)).'">
<input type="submit" value="submit" name="submit_btn">
</form>';
}
if(isset($_REQUEST['submit_btn']))
{
$user_name= $_POST["user_name"];
$a_random_password = $_POST["a_random_password"];
echo "Your User Name is:". $user_name;
echo "<br> and your Password is : $a_random_password;
}
?>
And my post.php
<?php
$url = "http://test.com/form.php";
$data = array(
'user_name' => 'John',
'a_random_password' => 'xxx',
'submit' => 'submit'
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo "Error"; }
var_dump($result);
?>
So how do i get the a_random_password value and submit it along with the form in a single request, else the password wont fit with the user name.
Sup, what you wanna do is impossible using PHP in that way, cause the only job PHP has is to render your content on server side. To listen to the client side you'll need to use javascript.
let randomPassword = document.getElementById('a_randow_password')
let name = document.getElementById('name')
let password = document.getElementById('password')
name.onkeyup = function(){
password.value = randomPassword.value + name.value
}
<input type="text" id="name" name="user_name" placeholder="name"><br />
<input type="hidden" name="a_random_password" id="a_randow_password" value="xxx">
<input type="text" id="password" placeholder="password"><br />
Not sure exactly what you trying to achieve, but if You want to interrupt a request (and modify|use it) from HTML to PHP, You need AJAX request (JavaScript).

Why textarea is showing old content on form submition?

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">

in wordpress get post methods does not work

I have made a site in Wordpress.
In my front-page.php and in my index.php files I entered both post and get methods just to echo a message in page-message.php file.
When I click the subscribe button the posts page:news appears.The posts page:news is defined in my Wordpress settings: dashboard=>settings=>reading=>Front page displays=>a static page.
In page-message.php I wanted to show the data I had entered from this form. T
Code for the forms:
<h2>Sign to Newsletter</h2>
<form action="page-message.php" method="post">
Name:<input class="input" type="text" name="name" value=""/><br/>
Email:<input type="text" name="email" value=""/>
<br/><input class="submit" type="submit" value="Subscribe"/>
</form>
Code for page-message.php:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
echo 'Congratulations!You have been successfully subscribed to our newsletter' .$name .$email;
?>
When I click the subscribe button my url changes to http://localhost/wordpress/page-message.php?name=stergios&email=something%40someone.com.
This seems to be correct when using the post method. The problem is the content!
It always loads the news (posts page from settings =>reading) and not the simple echo message?
It always shows the posts page when I write anything beyond my pages in the url. For example the url localhost/wordpress/hhhhhhhhhhh (a page that does not exist) does load the posts page and not a message error!
if "page-message.php" is a custom template in your theme you can get the page ID where it is assigned to with following query:
$get_message_pageId = new WP_Query(array(
'post_type' => 'page',
'meta_key' => '_wp_page_template',
'meta_value' => 'page-message.php'
));
and then you can set the action with:
action="<?php echo esc_url( get_permalink( $get_message_pageId->posts[0]->ID ) ); ?>"
so you will always have the correct URL in your form, but it will only work if you have 1 page where this template is assigned to
I know the reason why it is not working.
action="page-message.php"
your static page's path should be /wp-content/themes/{themename}/page-message.php u can try it,
action="<?php echo get_template_directory(); ?>/page-message.php"
But I don't think it will work. You can try
action="/index.php"
It is untested. let me know if it work.
Update: As you set that page as front page, then your wordpress root url will be that page's url (example.com or example.com/wpdirectory). Then simply do it,
action="/"
or
action="/wpdirectory"

get data from text area and post to a wall onfacebook

not sure if this is possible, but what i need to do is take the data from my text area and let the user post that to their wall.
my code snippet
<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" />
</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);
?>
the default message 'Hello World' posts to the wall, but i would like to replace that with the text in 'status2' text area. Is this possible?
Thanks
<textarea name="status2" cols="50" rows="5"<input type="text"/>
Doesn't make sense.
Guessing that your textarea is well coded in your real snippet. You should have the value of the textarea inside $_GET['status2'], so change traslate.php:
$args = array(
'message' => $_GET['status2']
...
this is the code i've written for the test:
index.html
<form method="GET" action="server.php">
<textarea name="status2"></textarea>
<input type="submit" value="go"/>
</form>
server.php
<?
print_r($_GET);
?>

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