6 Replies - 596 Views - Last Post: 17 February 2013 - 06:56 PM

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

onclick in php form?

Posted 17 February 2013 - 04:12 PM

onclick works:
<input type="submit" id="add" name="add" onclick="style.display='none';" />


but if i put it in a php form, it doesn't?

<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<input type="submit" id="add" name="add" onclick="style.display='none';" />
</form>


the button disappears, but comes right back?

Is This A Good Question/Topic? 0
  • +

Replies To: onclick in php form?

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3077
  • Posts: 10,785
  • Joined: 08-August 08

Re: onclick in php form?

Posted 17 February 2013 - 04:22 PM

That's because onclick is doing two things:
  • It sets the display to none.
  • It submits the form to the server, which runs the same script again, sending the original page back to the client, where the form is displayed.


Try:
<input type="submit" id="add" name="add" onclick="style.display='none'; return false;" />



By the way, there is no such thing as a PHP form. What you have is an HTML form that submits to the server requesting that it run a PHP script.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: onclick in php form?

Posted 17 February 2013 - 04:29 PM

onclick is Javascript, not PHP.

** Moved to Javascript **
Was This Post Helpful? 0
  • +
  • -

#4 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: onclick in php form?

Posted 17 February 2013 - 04:35 PM

View PostCTphpnwb, on 17 February 2013 - 04:22 PM, said:

That's because onclick is doing two things:
  • It sets the display to none.
  • It submits the form to the server, which runs the same script again, sending the original page back to the client, where the form is displayed.


Try:
<input type="submit" id="add" name="add" onclick="style.display='none'; return false;" />



with "return false;", the php is never executed.

This post has been edited by g37752: 17 February 2013 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3737
  • View blog
  • Posts: 13,065
  • Joined: 12-December 12

Re: onclick in php form?

Posted 17 February 2013 - 05:08 PM

What are you trying to do? If the submit button is used (as it should be) to submit the form then trying to hide it won't work as the page will be re-loaded. You would need to write PHP-code in the page that hides the submit button when the form is submitted.
Was This Post Helpful? 1
  • +
  • -

#6 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: onclick in php form?

Posted 17 February 2013 - 06:46 PM

View Postandrewsw, on 17 February 2013 - 05:08 PM, said:

What are you trying to do? If the submit button is used (as it should be) to submit the form then trying to hide it won't work as the page will be re-loaded. You would need to write PHP-code in the page that hides the submit button when the form is submitted.


how do you use php to hide the submit button?
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3737
  • View blog
  • Posts: 13,065
  • Joined: 12-December 12

Re: onclick in php form?

Posted 17 February 2013 - 06:56 PM

if (isset($_POST['add'])) {
    // the form was submitted.. don't insert the button
    // or, preferably, don't include the form at all!
    // or insert the button, but give it a class whose 
    // css-rule includes display:none
} else {
    echo "<input type=\"submit\" id=\"add\" name=\"add\" />";
}

although it is preferable not to use the submit button to confirm form-submission - I would add a hidden input to do this.

This post has been edited by andrewsw: 17 February 2013 - 06:57 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1