?
Solved

PHP/javascript self submit and refresh

Posted on 2003-02-20
12
Medium Priority
?
925 Views
Last Modified: 2008-03-17
How to refresh a self submitted form in order to up to date array of records in the same form ?

My form displays a list of records provided by PHP/MySQL select query.
Through this form, user can then enter an ID of record he wants to delete and then submit form to itself where there's a PHP/MySQL delete query.

It works fine but once submit button is pressed the array of course is not updated.
I suppose it should take a javascript "window.history.go(0)" command to rerun PHP/MySQL select query and
redisplay the page.
But where to put it ? If I put this JS command at the end of PHP statement,it's in fact inserted at the beginning of the resulting html page and of course doesn't work.
Is there a solution ?
0
Comment
Question by:kouri
12 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 7989051
Maybe you could do something like this:

var url = location.href
url += '?random='+Math.random();
location.href = url;

0
 

Author Comment

by:kouri
ID: 7989811
I don't understand Batalf.

Where will I need to put this piece of code  ?

And what does it do ?
It seems to be too much tricky for me :-)
0
 

Author Comment

by:kouri
ID: 7989845
I don't understand Batalf.

Where will I need to put this piece of code  ?

And what does it do ?
It seems to be too much tricky for me :-)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:carchitect
ID: 7991768
pls write this code on top of your file and try again....
regards
<?
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");                          
?>
0
 

Expert Comment

by:Zaytsev
ID: 7993713
I would suggest you to try the following:

<?php
$GLOBALS['now'] = gmdate('D, d M Y H:i:s') . ' GMT';
header('Expires: 0'); // rfc2616 - Section 14.21
header('Last-Modified: ' . $GLOBALS['now']);
header('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
header('Pragma: no-cache'); // HTTP/1.0
// Define the charset to be used
header('Content-Type: text/html; charset=' . $GLOBALS['charset']);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
     <title>Test</title>
</head>

<script>
function resubmit() {
 eval("document.myform.submit();");
}
</script>

<body onLoad="javascript:setTimeout('resubmit()',3000);">

<form action="form.php" name="myform" id="myform">
<input type="text" name="text" value="<?php echo $text?>">
<input type="submit" name="post">
</form>

</body>
</html>

The first part will avoid the page cacheing and then, the form "myform" will be resubmitted every 3000 ms.

I hopy that is what you were searching for.
0
 

Author Comment

by:kouri
ID: 7993911
Thx guys for your help !!

 @carchitect : I put your code at the end of the first php block (php block in charge of record deletion once form submitted) but doesn't change anything: the form is still not refreshed after submission; I still need to resubmit it a second time to see records changes on screen :-(

@zaytsev : of course it'll work if form become timer controlled but it is an entry form too, so I can't let a refresh automatically collide with a user entry..

I added in my form a button firing "window.history.go(0)" on click event. It works great but how to mix these two buttons ( submit one + refresh one)

As I explained if I add it at the END of PHP submit block; the JS refresh statement (window.history.go(0)) will then appear at the very BEGINNING of my HTL page ?!?!?


0
 

Expert Comment

by:Zaytsev
ID: 7997968
You can try this:

<script type="text/javascript" language="JavaScript">
<!--
function post_onclick() {
  eval("document.myform.submit(); window.history.go(0);");
}
//-->
</script>

<body>

<form action="form.php" name="myform" id="myform">
<input type="text" name="text" value="<?php echo $text?>">
<input type="submit" name="post" onClick="post_onclick()">
</form>

However all this window.history.go(0) staff seems for me strange. Something like javascript:location.reload(); should be better.

If this does not help, could you please explain your problem more clearly?
0
 

Author Comment

by:kouri
ID: 7998138
Hi Zaytsev!

Sorry doesn't work better.
location.reload is as good as history.go(0). In both cases, form is refreshed (through a button for example...)
But your way to associate submit and refresh in a javascrit function doesn't work :-(


Here's structure of the page:

<PHP>
-build and run a select query here
-form submission start here
-build and run a delete query here
</PHP>
<HTML>
<HEAD>
<JAVASCRIPT>
function post_onclick() here
</JAVASCRIPT>
</HEAD>
<BODY>
...

<FORM...action="<?php $PHP_SELF ?>"
<PHP>
here is listing of records provided by select query ( listing must be in the form because I need to put data on textfields in order to scan them through form.elements array)
</PHP>
here a special textField for user to enter record id to delete
submit button  with onClick="post_onclick()" here
</FORM>
</BODY>
</HTML>

my form, once submitted calls a PHP in order to fire the delete query;so I have no choice to submit with PHP but as you can see if I add the refresh command (mine or your) at the end of this PHP submit section in order to update list of records; it finally appears at the beginning of the page so it is not executed !

Maybe there's no solution and I have to split my form
One out-form for the listing (select)
one in-form for user to indicate which record to delete


0
 

Accepted Solution

by:
Zaytsev earned 200 total points
ID: 7998995
1) Why would not you just add

<PHP>
-build and run a select query here
-form submission start here
-build and run a delete query here
-> build and run a new select query here
</PHP>

2) You could do this as well

<PHP>
-build and run a select query here
-form submission start here
-build and run a delete query here
-> if actually something was deleted just send a <>header("Location: $PHP_SELF");<> PHP command to force user browser to reload the page and get the updated data
</PHP>

HTH,
Yury.
0
 

Author Comment

by:kouri
ID: 7999237
Your last proposition is nearly good but http fails to reload the page.It starts to do it then get an err 404.
I don't see why. Maybe my meta ?

Here's how it looks like now:

<PHP>
build and run a select query here
if form submission {
  if deletion {
     build and run a delete query here
     header("Location: $PHP_SELF");
}
</PHP>
<html>
<head>
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Pragma" content="no-cache">
0
 

Author Comment

by:kouri
ID: 7999603
Your last proposition is nearly good but http fails to reload the page.It starts to do it then get an err 404.
I don't see why. Maybe my meta ?

Here's how it looks like now:

<PHP>
build and run a select query here
if form submission {
  if deletion {
     build and run a delete query here
     header("Location: $PHP_SELF");
}
</PHP>
<html>
<head>
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Pragma" content="no-cache">
0
 

Author Comment

by:kouri
ID: 7999629
Your last suggestion was OK !
$PHP_SELF didn't return the full URL and I just had to add session ID to make header(location:...) work.

I should learn a little more about PHP and HTTP protocol

Thx Yury
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question