SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot dbworm's Avatar
    Join Date
    Apr 2003
    Location
    Lausanne
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP function call from a form?

    I have two questions:

    1) How can I call a PHP function from the "onClick" event of an <input type="button" onClick=" ... />

    2) How do I invoque a JavaScript alert box from inside a PHP function?

    I've tried a million things to no avail.

    Thanks, Dan

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2002
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. You need to pass a variable to PHP and then execute the function
    2.
    PHP Code:
    <?php
    function test()
    {
    ?>
    <script language="JavaScript">
       alert( "Hello!" );
    </script>
    <?php
    }
    test();
    ?>

  3. #3
    SitePoint Zealot dbworm's Avatar
    Join Date
    Apr 2003
    Location
    Lausanne
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nucleuz
    1. You need to pass a variable to PHP and then execute the function
    Yes, that much I figured...

    Here is what I want to do but don't know how:
    PHP Code:
    <form ....
    <input type="text" name="hisname" size="20" value="" />
    <input type="button" name="btn" value="Validate" onClick="<?php $retval test(this.hisname.value)" ?> />

    ... and the function
    <?php
    function test(name)
    {
      return name
    }
    ?>
    so that $retval will hold hisname and is available for further treatment.

    Would you be so kind as to supply a code snippet for the part inside the onClick? I am completely lost.

    Thanks, Dan

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You would have the script call itself, use:

    <form action="'.$_SERVER['PHP_SELF'].'" method="post"...>

    The code would read something like this:

    Test for a submission,
    if there was no submission, display the form,
    if there was a submission, test the submitted data and perform the manipulation.
    display a "success" message.
    Last edited by PHP John; May 27, 2003 at 09:02.
    John

  5. #5
    SitePoint Zealot dbworm's Avatar
    Join Date
    Apr 2003
    Location
    Lausanne
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP John
    You would have the script call itself, use:

    <form action="'.$_SERVER['PHP_SELF'].'" method="post"...>

    The code would read something like this: etc.
    Thanks PHP John.

    The problem is that the form action is already handling the action for the submit button of the same form.

    My question could be simply formulated like this: How would you write just this section so it works?

    onClick="<?php $retval = test(this.hisname.value)" ?> ...

    Where (this.hisname.value) is the value entered in an input type=text of the form and test() is the php func.

    Dan
    Last edited by dbworm; May 27, 2003 at 10:46.

  6. #6
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't use PHP with onclick events because onclick events use client-side code such as Javascript, and PHP is all server side.

  7. #7
    SitePoint Zealot dbworm's Avatar
    Join Date
    Apr 2003
    Location
    Lausanne
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Phil.Roberts
    You can't use PHP with onclick events because onclick events use client-side code such as Javascript, and PHP is all server side.
    Clear enough.

    I finally put that input field in a separate form and called PHP_SELF in action as you recommended in post#4. Works like a charm.

    Thanks, Dan


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •