Content disappears after refreshing of page (Wordpress) - php

I am developing a plugin where i have a form and a submit button. The form-action is the_permalink(). When the user clicks on the submit-button, a message ("hello") should be visible, which works fine with this code:
if(isset($_POST['submit']))
{
display();
}
function display()
{
echo "hello";
}
But if i refresh the page, the message is gone. Does anybody know how to fix this to that the message stays there?
This has porbably something to do with that i compare $_POST with 'submit', which is not given after a refresh...
Thanks for your help!

Please try following code
session_start();
if(isset($_POST['submit']))
{
$display_val = display();
$_SESSION['display_val'] = $display_val;
}
if(isset($_SESSION['display_val']))
{
echo $_SESSION['display_val'];
}
function display()
{
return "hello";
}

Related

How to create a html button that runs a php script?

i want to create a button in html that runs a php script,
i have this script:
<?php
$user = JFactory::getUser();
$userToken = JSession::getFormToken();
if (!$user->guest) : ?>
Log out
and i want to use it in a html button
any help?
call a js function onclick of your button and send ajax request on your script file to run php script...
<input type="button" value="Run Script" onclick="run_script();"/>
<script>
function run_()
{
$.post("yourScripFile.php",function(data){
if(data != null)
{
alert(data);
}
});
}
</script>
And in your php script file
<?php
$user = JFactory::getUser();
$userToken = JSession::getFormToken();
if (!$user->guest)
{
echo 'Log out';
die;
}
else
{
echo "else code here"; die;
}
?>
You can use a $_POST or $_GET parameter and pass it with the click of the button. Just put the script in an if block in the same page if($_GET['act'] == "logout"){//run script} and add ?act=logout to your url when you want to trigger the script. Instead of $_GET you can use $_POST and a hidden input named act

submit form only on button click not on page refresh

Below is my code, and is working fine.. The only problem is that when I refresh the page , It execute the success query.
<form action="" method="post">
<input type="submit" value="Purchase" id="btn" name="link_credits">
</form>
if(isset($_REQUEST['link_credits']))
{
echo "success";
//some codes here with sql statements
}
Else
{
echo " Please try Again tomorrow";
}
?>
Problem:
I don't want to run the query with page refresh, it should run the code only at button click.
Just add the line unset($_REQUEST['link_credits']); in success loop
if(isset($_REQUEST['link_credits']))
{
echo "success";
//some codes here with sql statements
if($success){
$_SESSION['msg']="Data Added successfully";
}
else{
$_SESSION['msg']="Data Not Added successfully.Please Check";
}
unset($_REQUEST['link_credits']);
header("Location:--somepage.php--");
}
else
{
echo " Please try Again tomorrow";
}
Then you can show the $_SESSION['msg'] value in --somepage.php-- and after showing use unset($_SESSION['msg']);
Your browser should be warning you that a page refresh will re-submit the form. So, you are clicking a button even when you refresh the page, or you are using a broken browser.
All that being said, here are some possible solutions:
Cookie the browser during the "success" phrase, and only allow the success condition to run if the cookie is not set/present.
Learn about "Redirect after POST", which is what you should be doing here. Essentially, you perform the success action, set flags as apropos, and forward the browser (via PHP's header()) to a page that says, "OK, it worked!".
Change your if condition to.
if(isset($_POST['link_credits'])){
echo "success";
//some codes here with sql statements
}
else
{
echo " Please try Again tomorrow";
}
This is because you are submitting the form to the same page with the method post, so when you refresh the page it novamento sent another request post.
Solution:
if(isset($_REQUEST['link_credits'])) {
echo "success";
$_REQUEST['link_credits'] = null;
} else {
echo " Please try Again tomorrow";
}
This happen because of simply on refresh it will submit your request again.
So the idea to solve this issue by cure its root of cause.
I mean we can set up one session variable inside the form and check it when update.
if($_SESSION["csrf_token"] == $_POST['csrf_token'] )
{
// submit data
}
//inside from
$_SESSION["csrf_token"] = md5(rand(0,10000000)).time();
<input type="hidden" name="csrf_token" value="
htmlspecialchars($_SESSION["csrf_token"]);">

why my echo doesn't work before a header function header(location:home.php);

why my echo doesn't work here in this code?It goes directly to the function header().
if($result){
echo("<script> alert('User Successfull Added')</script>");
header('location:home.php');
}
In order to make your code example work, you need to put it inside an if/else condition like so, if you want it to work, but since I only see what you posted as code, am putting this in as an example:
<?php
if($result){
echo("<script>alert('User Successfully Added')</script>");
echo("<script>window.location = 'home.php';</script>");
}
else {
header('Location: elsewhere.php');
}
Actually thy echo is working. but we cant see the message because at the same time the page redirects to the home.php page.
If you want to alert to display the alert message use jquery post. or php session variable
Assuming you want to both display the alert & redirect to home.php, why don't you just try setting a SESSION (or other global variable) and then check if it's set at the start of the redirected page?
Such as:
<?php
session_start();
$_SESSION['usercreated']="Y";
header('Location: elsewhere.php');
?>
and then on home.php at the start of your php code:
<?php
session_start();
if(isset($_SESSION['usercreated'])){
echo("<script> alert('User Successfully Added')</script>");
}
//....
?>
Sorry, I can't add comment here. But I think what you want to do needs Java and replace alert() with confirm()
var r=confirm("User Successfull Added")
if (r==true) // if user click OK
{
window.location.assign("home.php")
}
else // user click Cancel
{
}

play sound clip when php/mysql if statement is true?

hi i have a message alert box that appears when a user gets a new message, what i want to do is add sound to this box when it pops up, i am using a php if statement to check when a user gets a new message and i have tried adding sound by doing the following but its not working please can someone show me how to do this. thanks.
<?php
$check_new_chats = check_new_chats();
while ($chat = mysql_fetch_array($check_new_chats))
if (isset($_SESSION['user_id'])) {
if ($chat['to_user_id'] == $_SESSION['user_id']){
echo( "<embed name='sound_file' src='/assets/music/sound_file.mp3' loop='true' hidden='true' autostart='true'/>"); ?>
<?php
$check_new_chats = check_new_chats();
while ($chat = mysql_fetch_array($check_new_chats))
if (isset($_SESSION['user_id'])) {
if($chat['to_user_id'] == $_SESSION['user_id']){
echo '<script type="text/javascript">play_sound();</script>';
}
}
?>
Here's the Javascript function:
<script type="text/javascript">
function play_sound() {
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', '/assets/music/sound_file.mp3');
audioElement.setAttribute('autoplay', 'autoplay');
audioElement.load();
audioElement.play();
}
</script>
According to the question:
hi i have a message alert box that appears when a user gets a new
message, what i want to do is add sound to this box when it pops up
You already have some sort of javascript function that displays an alert box when needed. Use the info from this answer to play a sound with it.
<audio id="soundHandle" style="display: none;"></audio>
<script>
soundHandle = document.getElementById('soundHandle');
soundHandle.src = '/assets/music/sound_file.mp3';
</script>
//With your message alert function....
alert("Message box");
soundHandle.play();

Reload page in php

While designing sign up page I wish to reload page if user has submitted anything wrong in the fill up boxes. How can I reload page after the user presses submit button with a wrong entry ? (while verifying expected content through php)
You can use header() plus some _GET variables for telling the original page there were errors.
Form submit page:
<?php
//.... lots of code validation
if ($failed) {
header('Location: http://path.com/to/your/site/original_form.php?error=1');
}
?>
Form page:
<?php
if (isset($_GET['error']) {
echo "there was an error! Please fix it!";
}
?>
Try the following -
if (validation_failed) {
header("Location: http://yourserver.com/signup.php");
} else {
header("Location: http://yourserver.com/welcome.php");
}
PHP Header function

Categories