This topic is locked

Button to save and redirect

6/16/2006 8:49:10 PM
PHPRunner General questions
D
Dale author

Im having a terrible consistency problem. I am trying to add a button on the add and edit pages to Save and then redirect back to the list page it came from.
I have added a extra button named "Save and Close" to the edit form as
<input class="button" type="button" onclick="submit_form();" value="<?php echo mlang_message("SAVE");?>&nbsp;&amp;&nbsp;<?php echo mlang_message("CLOSE");?>">

// I have added this script just below the button and I am sure this is where the problem lies.

<script language="javascript">

function submit_form()

{

document.editform.submit();

}

</script>
the form has the onSubmit="return validate();" function in the form statement.
When i bring up the form. I can make a change, hit the original Save button and it works everytime and all the time.
when I bring up the form and make a change then

select my new button, it works once, then it doesnt, then it does.
I have been trying all day to get it consistent.
What magical thing am I missing here to make it consistently save when using the new button.

Once I can get it to act exactly as the orignal save button and can then redirect to the page I want.
Any help will be appreciated. I hope this is clear enough.

D
Dale author 6/19/2006

I guess this might have been a stupid question.

After seeing no response over the weekend I went searching for an answer.
Very quickly I learned that using the form.submit in javascript does not invoke the onSubmit command of a form.
So now Im off to find an answer to my javascript inexperience on how to use the return validate in a script and then do my other steps to redirect and reload the calling page.
If anyone has a tip for me that would be great. If not, well, thanks for letting me ramble.

Admin 6/20/2006

Dale,
I'm sorry for the delay with reply.

Here is the sample code you can use to add "Save and Close" button.

The button code:

<input class="button" type="button" onclick="submit_form();" value="save and close">

<input type=hidden name="backtolist" value="">

<script language="javascript">

function submit_form()

{

if(!editform.onsubmit || editform.onsubmit())

{

editform.backtolist.value="yes";

document.editform.submit();

}

}


Then find this line in ..._edit.php file:

$message="<div class=message><<< "."Record updated"." >>></div>";



and insert this snippet just after:

if(@$_POST["backtolist"])

{

header("Location: ..._list.php?a=return");

return;

}

D
Dale author 6/20/2006

Perfect. Thankyou thankyou.

I had come across the header location theory in my reading and tried a quick attempt but I had no success.

The code sample is very much appreciated.
Im off to try it out.

D
Dale author 6/20/2006

Alexa,

The snippet works perfectly but of course I have add a wrinkle.
I have modified the code that phprunner creates for the list.php.

When you select a row, a div is opened and the request for the ..._edit.php or ..._add.php is requested and then displayed in this new dynamic window.
What I was using the Save and Close button for was to Save the change, then close the dynamic window and refresh the ..._list page.
the snippet below, you provided does refresh the page, but it stays in the opened window.
if(@$_POST["backtolist"])

{
header("Location: ##SHORTTABLENAME##_list.php?a=return");

return;

}
I thought I could add the close window statement as below to close the window and then run the refresh.
if(@$_POST["backtolist"])

{

window.top.hidePopWin();

header("Location: ##SHORTTABLENAME##_list.php?a=return");

return;

}
I get the PHP error happened.
Use of undefined constant window - assumed 'window'
I have been searching for an answer on the net for over an hour and I have not come up with a solution.
Can you spot what I am doing wrong, or give me a tip on solving this issue.

How do i get the window to close and run the refresh in one swoop.
Thankyou for you experience and patient.

Any help is greatly appreciated.

D
Dale author 6/21/2006

Solution found. I added functionality to the dynamic window created to run a script when closing if True is returned.
This ensures the list is refreshed when closing the dynamic window. This now allows me to add and edit any record in a separate window without leaving the list. When you close the window you are right back to the list.
Now if I can only get a next and back button working to work through the list without having to go back to the list. That would be great.
IF anyone has any ideas on creating a next and back to traverse through a list. That would be fantastic.

D
drh 10/6/2006

Hello forum members,
I seem to be having problems trying to get this to work. I am running a phprunner 3.0 build 118 generated app. After editting a record, I want to redirect to the list page without having to click the "back to list" link.
As I was searching the forum, this post is very close to what I want to be able to do. I have a custom javascript file (pcmacct_js.php) in my include directory. It does things like disabling certain fields based on the record contents, record age, etc. It also does some custom formatting for telephone numbers, etc.
Since I already have this custom javascript file I want to put the advised code in this file instead of the main ..._edit.php program.
This is what I did. The code for the button in ..._edit.php is this:
<!-- buttons -->

<tr height=50><td colspan=2 align=center>

<input class=button type=button value="Save" onclick="java script: submitform()">

<input type=hidden name="backtolist" value="">

<input class=button type=reset value="Reset">

<input type=hidden name="a" value="edited">

<input type=hidden name=imagefield value="">

</td></tr>
I added this per the instructions to the ..._edit.php
$message="<div class=message><<< "."Record updated"." >>></div>";^M
if(@$_POST["backtolist"])

{

header("Location: mos_pcmacct_list.php?a=return");

return;

}
Since I don't want to put the javascript in my edit.php program as this example did I placed the following in my function submitform() in the include/custom_java_script.php program.
<script type="text/javascript">

color = "#F1F1F1";
document.editform.value1.disabled = 1;

document.editform.value1.style.background = color;

document.editform.value2.focus();
function submitform()

{

document.editform.value1.disabled = '';

if(!editform.onsubmit || editform.onsubmit())

{

editform.backtolist.value="yes";

document.editform.submit();

}

}

</script>
Now when I run the ..._edit.php program and click save I get the "Cannot modify header information - headers already sent by" error message. Here is the entire message:
Error type 2

Error description Cannot modify header information - headers already sent by (output started at /var/www/vhosts/cbs-checkmate.com/httpsdocs/phprunner/admin/include/pcmacct_js.php:1)

URL cbs-checkmate.com/phprunner/admin/mos_pcmacct_edit.php?

Error file /var/www/vhosts/cbs-checkmate.com/httpsdocs/phprunner/admin/mos_pcmacct_edit.php

Error line 322
line 322 is this:

header("Location: mos_pcmacct_list.php?a=return");
this line was added per above right after the "Record Updated" line.
I should add, if I follow the instructions per this thread exactly (by not using my custom javascript program) and just adding the suggested code, it works. However, then I don't get the benefits of my custom javascript. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=11667&image=1&table=forumreplies' class='bbc_emoticon' alt=':(' />
I know this error message is a common one.
Anyone any thoughts?
Thanks,

Dave

Admin 10/9/2006

Dave,
the redirect fails because of your pcmacct_js.php file.

Either move the include statement for this file after the redirection code in ..._edit.php or use Javascript code to redirect.

I.e. replace

header("Location: mos_pcmacct_list.php?a=return");



with the following:

?>

<script language="javascript">

window.location = "mos_pcmacct_list.php?a=return";

</script>

<?php

D
drh 10/9/2006

Thanks Alexey,

That worked perfectly. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=11724&image=1&table=forumreplies' class='bbc_emoticon' alt=':D' />
Dave