Problems with form submition

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Problems with form submition

 
0
  #11
Apr 4th, 2008
eg. document.formname.submitButtonName.submit();
I dont think you can. But you can do this. Instead of having a submit button for delete button, have a normal button as DangerDev has mentioned. Then onclick call a javascript function and submit the page. Eg.
  1. <?php
  2. print_r($_POST);
  3. ?>
  4. <html>
  5. <head>
  6. <script type="text/javascript">
  7. function delsubmit() {
  8. var conf;
  9. conf=confirm("Are you sure ?");
  10. if(conf) {
  11. document.test.submit();
  12. } else {
  13. return false;
  14. }
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <form name="test" method="post" action="<?php echo $PHP_SELF; ?>">
  20. <input type="text" name="name" value="1">
  21. <input type="button" name="button" value="Delete" onclick="javascript: return delsubmit();">
  22. </form>
  23. </body>
  24. </html>
This is just a simple example. The next snippet of code will not work, because there is already a submit button. Eg.
  1. <?php
  2. print_r($_POST);
  3. ?>
  4. <html>
  5. <head>
  6. <script type="text/javascript">
  7. function delsubmit() {
  8. var conf;
  9. conf=confirm("Are you sure ?");
  10. if(conf) {
  11. document.test.submit();
  12. } else {
  13. return false;
  14. }
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <form name="test" method="post" action="<?php echo $PHP_SELF; ?>">
  20. <input type="text" name="name" value="1">
  21. <input type="submit" name="submit" value="add">
  22. <input type="button" name="button" value="Delete" onclick="javascript: return delsubmit();">
  23. </form>
  24. </body>
  25. </html>
To make both of them work, you should have 2 buttons. Eg.
  1. <?php
  2. print_r($_POST);
  3. ?>
  4. <html>
  5. <head>
  6. <script type="text/javascript">
  7. function delsubmit() {
  8. var conf;
  9. conf=confirm("Are you sure ?");
  10. if(conf) {
  11. document.test.submit();
  12. } else {
  13. return false;
  14. }
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <form name="test" method="post" action="<?php echo $PHP_SELF; ?>">
  20. <input type="text" name="name" value="1">
  21. <input type="button" name="button" value="add" onclick="javascript: document.test.submit();">
  22. <input type="button" name="button" value="Delete" onclick="javascript: return delsubmit();">
  23. </form>
  24. </body>
  25. </html>

If you still have problems, let me know!
Cheers,
Naveen
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Problems with form submition

 
0
  #12
Apr 4th, 2008
Thanks for that nav33n. Slowly getting somewhere, the form is submitting but it's not actually executing the php code. Is my php code below correct?:

  1. if (isset($_POST['FormName'])) {
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Problems with form submition

 
0
  #13
Apr 4th, 2008
Nope.. $_POST['formname'] will not hold any value. You can do it this way (again, as DangerDev mentioned), have a hidden variable (task). When you click on 1st button, assign a value to task variable. document.formname.task.value='add'; Similarly, assign another value when another button is clicked.
document.formname.task.value='delete'; Then you can check what value task variable holds.
  1. if($_POST['task']=="add"){
  2. //add
  3. }
  4. if($_POST['task']=="delete"){
  5. //delete
  6. }
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Problems with form submition

 
0
  #14
Apr 4th, 2008
Still having problems executing the php code. this is what I have so far:

Javascript for setting the task value to del and then submitting the form
  1. function confDelete() {
  2. var answer = confirm("Are you sure")
  3. if (answer){
  4. document.formName.task.value=='del';
  5. document.formName.submit();
  6. } else {
  7. return false;
  8. }
  9. }

The hidden "task" field and the delete button
  1. <input type="hidden" name="task" id="task" value="">
  2.  
  3. <input onclick="javascript: return confDelete();" type="button" name="delete" value="Delete">

If task has the value of del then execute PHP code
  1. if ($_POST['task']=="del"){
  2. // execute code
  3. }

My second button is set up in a similar way. Right now neither of them execute the required PHP code.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Problems with form submition

 
1
  #15
Apr 4th, 2008
document.formName.task.value=='del';
Should be
document.formName.task.value='del';
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 154
Reputation: Suomedia is an unknown quantity at this point 
Solved Threads: 19
Suomedia Suomedia is offline Offline
Junior Poster

Re: Problems with form submition

 
0
  #16
Apr 4th, 2008
You would do far better not to rely on javascript for this at all. Instead you should parse the post with PHP and from that display a confirmation for the delete if delete was selected. That is simply good coding practice.

It is common to use javascript for error checking (I do myself) however the form should still submit and also be checked with PHP if javascript is disabled.


Matti Ressler
Suomedia
Last edited by Suomedia; Apr 4th, 2008 at 10:23 pm.
If you want your dreams to come true, the first thing you must do is to wake up....
Suomedia - Dynamic Content Management
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,627
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Problems with form submition

 
0
  #17
Apr 6th, 2008
Relying on Javascript to deliver the business functionality is dangerous to the business which you are trying to cater. If this is some school project, it doesn't matter anyways but if this is for real, you need to start rethinking your design and add a server side check for such critical functionality.
Last edited by ~s.o.s~; Apr 6th, 2008 at 2:25 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Problems with form submition

 
0
  #18
Apr 7th, 2008
Thanks nav33n. Got it working.

Suomedia, ~s.o.s~...Thanks for your comments. I realize javascript could be switched off on some machines and would never use this kind of functionality if it was being used by the public. It's a simple admin system that's only going to be used by one person.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Problems with form submition

 
0
  #19
Apr 7th, 2008
You are welcome
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC