Reload parent page after submit in iframe - javascript

I have the same issue as in this question. (which has no correct answer, I just test all of them):
Reload page after submit in iframe (javascript/jquery)
What I need is very well explained there.
I have a button in a form, all this inside an iframe (same domain) and I need to reload the parent page, just after the iframe has made the whole submit process, I do not want to "cut" it or interrupt it.
UPDATE:
I kept searching and I found this. But I can't understand the code in the correct answer, is that for asp? I'm on PHP.
Reload page after submit in iframe (javascript/jquery)
http://dotnetspidor.blogspot.mx/2011/07/refresh-parent-page-partially-from.html

Once the form has been submitted in the iframe
<form method="POST" action="getForm.php">
The page reloads into getForm.php, and in that file you do:
<?php
$input = $_POST['input'];
// process form and do your stuff etc
echo "<!DOCTYPE html>";
echo "<head>";
echo "<title>Form submitted</title>";
echo "<script type='text/javascript'>window.parent.location.reload()</script>";
echo "</head>";
echo "<body></body></html>";
?>
and that would reload the parent page after the form has submitted, now the only question is, why are you submitting through an iFrame if you're going to reload the parent page anyway.

all that you need to do is add the below line,
window.parent.location.reload();
at the end of the function that is called onclick of your submit button.

Related

how to execute a php function from a separate php file onClick

I have two php files (1.php and 2.php) linked with require
php.1 has a qr <img src="http://chart.googleapis.com/chart?chs=125x125&cht=qr&chl=<?php echo $_jattu; ?>" width="50%"> which gets its value from a string variable in 2.php $_jattu;
What I want is for <?php echo $_jattu; ?> to only echo when <a class="w3-button2 w3-black2"></a> is clicked and not when the page is loaded or refreshed, what can I do to achieve this?
This is imposible to do on the server side. Because the "onClick" event jumps on the client side when de user do the action.
You have few options.
Enable another URL for load the content of $_jattu and when the user click load it with an AJAX request. Is the best way to do it and the result is more smooth and user friendly
As you say you want to do it refreshing. So, slightly refresh the page with a new parameter on your url that tolds you that the "onClick" event has jump. Like:
.../your/path?hasClick=true
And in your php code:
if(isset($_GET["hasClick"]) && $_GET["hasClick"]){
echo $_jetty;
}
http://php.net/manual/en/reserved.variables.get.php
if you want to remember that the user has clicked "forever" you can setup a cookie.
http://php.net/manual/en/function.setcookie.php
You can use ajax. Make a request to whatever php script you want which will return $_jattu when clicking on the link. Then update your img link in javascript using the return of the ajax request.

How to remove a captcha?

I have add CaptchMe on my website.
Screenshot.
The problem is that there can be only 1 CaptchMe on the same page. The sidebar is include on each page.
The solution in the remove the captcha in the sidebar when the sidebar is closed, and the capcha on the page is displayed. If the sidebar is open with a click, the captcha on the page need to be removed and the capcha on the sidebar displayed.
How can I do it ?
To display the captcha, I use
<?php echo captchme_generate_html($publicKey, $error, $ssl, $customAttributes); ?>
If I add it in a div, I can remove the content with
document.getElementById("captcha").innerHTML = "";
But after, when the sidebar is closed, I can't display it again because the div is empty. And I can't do
document.getElementById("captcha").innerHTML = "<?php echo captchme_generate_html($publicKey, $error, $ssl, $customAttributes); ?> ";
Because this is impossible.
Thank you for the help.
To regenerate the captacha you should be able to put the captcha PHP into it's own file on the server.
Then use JQuery .load() on the php file to make an AJAX call and load the response HTML directly into the DOM.
http://api.jquery.com/load/
I have found a very easy solution after 5 hours ...
I add the login form with an iframe in the sidebar and the other captcha (left) in directly on the page.
The main page is a page and the iframe an other page.
And I can add 2 CaptchMe on the same page :)
Thank you for the help, I learn one more element in jquery :)

How to stop JavaScript alert from refreshing page?

I am teaching myself, so I apologize in advance if this is a 'newbie' question, but I have searched the internet and cannot find a solution.
So I've created a HTML form, and in that form I have added a Dropzone.js where people upload a file. When finished they click a button which saves it into a MySQL form, that all works great. The issue is when people click the button before the file finishes uploading. To counteract this issue, I've created an 'If Statement' that checks if the uploaded file exists. If it hasn't uploaded, then a JavaScript alert is triggered, which all works, but it refreshes the page, which erases all the imputed data.
// This is when the button is clicked
if(isset($_POST['Upload'])){
if (file_exists(blah blah) {
//All my MySQL stuff is in here
//If it isn't uploaded
} else {
echo '<script language="javascript">';
echo 'alert("Please upload at least one file")';
echo 'return false';
echo '</script>';
}}
Despite the 'return false'; it still resets the page?
Any help would be appreciated, thanks!
Can you show me the all code. You can do it with lots of methods i do it with validation. you can set a variable which increase on file upload. if variable value zero then return false form will not submit.
Php does not support like this echo 'return false'. you have to use JavaScript validation for this purpose.

PHP and window.close in JavaScript

I have a problem. I have a page that when you click a button, a popup with a form is shown. So, I complete some data and I submit. What I want to do is, to submit the form, close the form and refresh the parent page. I don't want to do it with AJAX.
The problem is that in my parent page I have to refresh content with the input information of the form.
So when I refresh, sometimes the data is shown and sometimes not. Do you know why this could happen?
I just use onsubmit="refreshParent()" in my form. The info is stored always in my database, so I think the problem may be that sometimes the refresh catches the new info and sometimes not.
function refreshParent() {
window.opener.location.reload();
window.close();
}
I use this to reload the page that opened a popup window:
<script language="JavaScript">
<!--
function reloadParentPage() {
window.opener.location.href = window.opener.location.href;
if (window.opener.progressWindow) {
window.opener.progressWindow.close()
}
window.close();
}
//-->
</script>
By the way, the code above is called by a link or button in the popup page.
You have a race condition between the script doing the insert and the script reloading the parent.
The solution is to call refreshParent on the page after the submit - that way you know the data is in the database. You don't even have to do it on document ready - return a stub page that just defines and calls refreshParent in the head tag.
In PHP when you run post script, at the end, include this code :
echo '<html><script language="javascript">
parent.location.href="http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'"; // or any other url
</script></html>';
This will output a javascript that will reload the windows.

How to call a javascript function once on page load, page reloads, don't call it again

On a Magento shopping cart, there are 2 forms that need to be filled out: shipping estimate and coupon code. Each refreshes the page after submitting the form. The shipping estimate form needs to be submitted before the coupon form so that the coupon can validate the country.
What I'd like to do is to call the shipping form's coShippingMethodForm.submit() on load. That way the country is loaded, and then the coupon form will check the country. This has the added advantage that the initial shipping rates are displayed without requiring the user to submit the form.
My challenge: if I call coShippingMethodForm.submit() on load - it will refresh the page and then continuously refresh b/c the coShippingMethodForm.submit() keeps submitting and refreshing.
How do I call that function once, page refreshes, and not run again (unless the user clicks the submit button).
Example Magento cart page I'm referring to
You can use cookie to prevent resumbit on reload.
Function js for broswer cookie can be found here http://www.quirksmode.org/js/cookies.html
In your JS function coShippingMethodForm.submit
coShippingMethodForm.submit = function(){
//check cookie here
if(readCookie('already_submit')==1)
return;
createCookie('already_submit', 1, 1); //for 1 day
//old code here
}
And on submit button, add eraseCookie line:
<input type="submit" id="your_submit_button"
onclick="eraseCookie('already_submit');" />
I see you're using PHP, why not just have a hidden form
<input type='hidden' name='countrySubmitted' value='true' />
and then when php renders the page check to see if the value is set, if not then call the function to submit.
echo "<script type='text/javascript'>";
if (isset($_POST['countrySubmitted']))
echo "document.coShippingMethodForm.Submit();";
echo "</script>";

Categories