Cannot pass form variable(s) to PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NetCoder

    Cannot pass form variable(s) to PHP

    Hi to all,

    I have made a script that uses both JavaScript and PHP. The script has a
    form and when it is submitted, a JavaScript confirmation box appears, asking
    the user to confirm their action. If they click No, the action is cancelled
    and if they click Yes, the form is submitted.

    If only it were that simple... The problem is that when the form is
    submitted using form.submit() within a JavaScript function, no form
    variables are being sent through the POST method, however, if I remove all
    JavaScript and then submit the form, everything works fine.

    The complications are that I need the confirmation boxes and I need to use a
    form, not just a hyperlink, because I need to pass the contents of a text
    field through the POST method too...

    Any ideas?


  • David Dorward

    #2
    Re: Cannot pass form variable(s) to PHP

    NetCoder wrote:
    [color=blue]
    > If only it were that simple... The problem is that when the form is
    > submitted using form.submit()[/color]

    Avoid submitting forms using JavaScript. If at all possible, use a regular
    submit button and perform any tests using the onsubmit event.

    <script type="text/javascript">

    function myFunction() {
    if (condition1) {
    return true;
    } else {
    return false;
    }
    }
    </script>

    <form action="foo" onsubmit="retur n myFunction()">
    <input type="submit">
    </form>


    --
    David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is

    Comment

    • Evertjan.

      #3
      Re: Cannot pass form variable(s) to PHP

      David Dorward wrote on 13 jun 2004 in comp.lang.javas cript:
      [color=blue]
      > if (condition1) {
      > return true;
      > } else {
      > return false;
      > }
      >[/color]

      return condition1;

      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      • NetCoder

        #4
        Re: Cannot pass form variable(s) to PHP


        "David Dorward" <dorward@yahoo. com> wrote in message
        news:cah5ou$sa1 $2$830fa795@new s.demon.co.uk.. .[color=blue]
        > NetCoder wrote:
        >[color=green]
        > > If only it were that simple... The problem is that when the form is
        > > submitted using form.submit()[/color]
        >
        > Avoid submitting forms using JavaScript. If at all possible, use a regular
        > submit button and perform any tests using the onsubmit event.
        >
        > <script type="text/javascript">
        >
        > function myFunction() {
        > if (condition1) {
        > return true;
        > } else {
        > return false;
        > }
        > }
        > </script>
        >
        > <form action="foo" onsubmit="retur n myFunction()">
        > <input type="submit">
        > </form>
        >
        >
        > --
        > David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
        > Home is where the ~/.bashrc is[/color]

        The above suggestion works, however, whichever button I click now, Yes or
        No, the form submits anyway...

        Here's the code I'm using:

        *** form.html ***

        <html>
        <head>
        <title>New Record</title>
        <script language="JavaS cript">
        function confirm_action( ) {
        confirm_text = confirm("Are You Sure?");
        if (confirm_text) {
        alert("Action Confirmed. Click OK To Continue...");
        }
        else {
        alert("Action Cancelled.");
        return false;
        }
        }
        </script>
        </head>
        <body>
        <form method="POST" action="process .php" onSubmit="confi rm_action();">
        <p><input type="text" name="product_c ode" size="8">&nbsp; <input
        type="submit" name="new_recor d" value="Insert Information"></p>
        </form>
        </body>
        </html>

        *** process.php ***

        <?php
        $new_record = $_POST["new_record "];
        if ($new_record) {
        $product_code = $_POST["product_co de"];
        if (!$product_code ) {
        echo "<p>No product code was specified. Please go back and type in a product
        code.</p>";
        }
        else {
        // Connect to MySQL server and insert product code into database.
        header("Locatio n: form.html");
        }
        }
        else {
        echo "<p>This file cannot be accessed directly.</p>";
        }
        ?>

        The above code, submit's the form whatever the user selects...


        Comment

        • David Dorward

          #5
          Re: Cannot pass form variable(s) to PHP

          NetCoder wrote:
          [color=blue][color=green]
          >> <form action="foo" onsubmit="retur n myFunction()">[/color][/color]
          [color=blue]
          > The above suggestion works, however, whichever button I click now, Yes or
          > No, the form submits anyway...[/color]
          [color=blue]
          > <form method="POST" action="process .php" onSubmit="confi rm_action();">[/color]

          There is a pretty significant difference between the two code snippits. You
          lost the "return" keyword somewhere along the line.

          --
          David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
          Home is where the ~/.bashrc is

          Comment

          • Andrew DeFaria

            #6
            Re: Cannot pass form variable(s) to PHP

            NetCoder wrote:
            [color=blue]
            ><?php
            >$new_record = $_POST["new_record "];
            >if ($new_record) {
            >$product_cod e = $_POST["product_co de"];
            >if (!$product_code ) {
            >echo "<p>No product code was specified. Please go back and type in a product
            >code.</p>";
            >}
            >
            >[/color]
            I know this is an example however wouldn't the above check bet better
            done in the JavaScript code? I mean why make a trip to the server if you
            can easily tell in the JavaScript that the field was not specified?
            --
            If you drink, don't park. Accidents cause people.

            Comment

            • Lee

              #7
              Re: Cannot pass form variable(s) to PHP

              Andrew DeFaria said:[color=blue]
              >
              >NetCoder wrote:
              >[color=green]
              >><?php
              >>$new_record = $_POST["new_record "];
              >>if ($new_record) {
              >>$product_co de = $_POST["product_co de"];
              >>if (!$product_code ) {
              >>echo "<p>No product code was specified. Please go back and type in a product
              >>code.</p>";
              >>}
              >>
              >>[/color]
              >I know this is an example however wouldn't the above check bet better
              >done in the JavaScript code? I mean why make a trip to the server if you
              >can easily tell in the JavaScript that the field was not specified?[/color]

              It should be checked both places.

              Checking on the client side should only be for the visitor's
              convenience. Anything that you care about needs to be checked
              again on the server, because some visitors may have Javascript
              disabled (and because it's easy to bypass client-side checks
              even without disabling Javascript).

              Comment

              • NetCoder

                #8
                Re: Cannot pass form variable(s) to PHP


                "David Dorward" <dorward@yahoo. com> wrote in message
                news:cahmkf$bvc $1$8300dec7@new s.demon.co.uk.. .[color=blue]
                > NetCoder wrote:
                >[color=green][color=darkred]
                > >> <form action="foo" onsubmit="retur n myFunction()">[/color][/color]
                >[color=green]
                > > The above suggestion works, however, whichever button I click now, Yes[/color][/color]
                or[color=blue][color=green]
                > > No, the form submits anyway...[/color]
                >[color=green]
                > > <form method="POST" action="process .php" onSubmit="confi rm_action();">[/color]
                >
                > There is a pretty significant difference between the two code snippits.[/color]
                You[color=blue]
                > lost the "return" keyword somewhere along the line.
                >
                > --
                > David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
                > Home is where the ~/.bashrc is[/color]

                Sorry about that, the eyes managed to miss that part! Well, tried it out and
                it works brilliantly (the way it should do!) so thanks very much!



                Comment

                Working...