How to process text from HTML form?

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

    How to process text from HTML form?

    This may be a basic question -

    I have a form on my web page that looks like this:

    <span id="site_search ">
    <form name="frmSearch " method="post" action="" style=" margin: 0 0 0
    15px ">
    Search This Site:
    <input type="text" name="search_fi eld"><input name="Go" type="submit"
    value="Go" style="font-family:Arial, Helvetica, sans-serif">
    </form>
    </span><br/>

    How do take the input from this form and use it to populate another form (on
    another page within the same site) when the user clicks the Go button?
    Also, how do I cancel any action if the form is empty?

    Thanks in advance.


  • eXeonical

    #2
    Re: How to process text from HTML form?

    deko wrote:[color=blue]
    > This may be a basic question -
    >
    > I have a form on my web page that looks like this:
    >
    > <span id="site_search ">
    > <form name="frmSearch " method="post" action="" style=" margin: 0 0 0
    > 15px ">
    > Search This Site:
    > <input type="text" name="search_fi eld"><input name="Go" type="submit"
    > value="Go" style="font-family:Arial, Helvetica, sans-serif">
    > </form>
    > </span><br/>
    >
    > How do take the input from this form and use it to populate another form (on
    > another page within the same site) when the user clicks the Go button?
    > Also, how do I cancel any action if the form is empty?
    >
    > Thanks in advance.
    >
    >[/color]

    The input from user can be found in eiher $_GET or $_POST arrays
    depending on the method your form uses. You should read
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    In you case you can use something like this:
    if (empty($_POST['search_field'])) {
    // Error message
    } else {
    // Search code
    }

    Comment

    • Geoff Berrow

      #3
      Re: How to process text from HTML form?

      I noticed that Message-ID: <cdtot1$f4s$1@p laza.suomi.net> from eXeonical
      contained the following:
      [color=blue]
      >The input from user can be found in eiher $_GET or $_POST arrays
      >depending on the method your form uses. You should read
      >http://www.php.net/manual/en/languag...predefined.php
      >
      >In you case you can use something like this:
      >if (empty($_POST['search_field'])) {
      > // Error message
      >} else {
      > // Search code
      >}[/color]

      And you populate a text box like this

      <input type="text" name="name"valu e="<?php echo $_POST['search_field'];
      ?>">
      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • Michael Fesser

        #4
        Re: How to process text from HTML form?

        .oO(deko)
        [color=blue]
        >I have a form on my web page that looks like this:
        >
        > <span id="site_search ">
        > <form name="frmSearch " method="post" [...][/color]

        span is an inline-element, you can't put block-elements like a form in
        it. Use a div or a more reasonable element instead.

        Micha

        Comment

        • Chung Leong

          #5
          Re: How to process text from HTML form?

          "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
          news:v7r4g09vhn rrem84a0grbi457 mfn6dt8ir@4ax.c om...[color=blue]
          > I noticed that Message-ID: <cdtot1$f4s$1@p laza.suomi.net> from eXeonical
          > contained the following:
          >[color=green]
          > >The input from user can be found in eiher $_GET or $_POST arrays
          > >depending on the method your form uses. You should read
          > >http://www.php.net/manual/en/languag...predefined.php
          > >
          > >In you case you can use something like this:
          > >if (empty($_POST['search_field'])) {
          > > // Error message
          > >} else {
          > > // Search code
          > >}[/color]
          >
          > And you populate a text box like this
          >
          > <input type="text" name="name"valu e="<?php echo $_POST['search_field'];
          > ?>">
          > --
          > Geoff Berrow (put thecat out to email)
          > It's only Usenet, no one dies.
          > My opinions, not the committee's, mine.
          > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]

          Don't forget to escape HTML special characters. Hence

          <input type="text" name="name"valu e="<?php echo
          htmlspecialchar s($_POST['search_field']); ?>">


          Comment

          • deko

            #6
            Re: How to process text from HTML form?

            > span is an inline-element, you can't put block-elements like a form in[color=blue]
            > it. Use a div or a more reasonable element instead.[/color]

            Thanks for the tip.


            Comment

            • deko

              #7
              Re: How to process text from HTML form?

              > And you populate a text box like this[color=blue]
              >
              > <input type="text" name="name"valu e="<?php echo $_POST['search_field'];
              > ?>">[/color]

              Hi and thanks for the reply.

              So, the receiving form should look like this:

              <INPUT TYPE=text value="<?php echo $_POST['search_field'];?>">
              <INPUT type=submit VALUE='Search'>

              But the receiving form is on a different page - how do I pass the value of
              $_POST to this other page? The idea is for the user to click Go on one page
              and then be taken to another page with another form which will be
              automatically populated with $_POST from the first page/form.

              I tried this on the sending form/page:

              <div id="site_search ">
              <form name="frmSearch " method="post" action="" style=" margin: 0 0 0
              15px ">
              Search This Site:
              <input type="text" name="search_fi eld"><input name="Go" type="submit"
              value="Go" style= "font-family:Arial, Helvetica, sans-serif">
              </form>
              <?php echo ($_POST['search_field']);?>
              <meta http-equiv="refresh" "csi_search.php "> <!-- Here I want to
              redirect to csi_search.php where the receiving form is. -->
              </div><br/>

              The value I entered in the form was echoed on the sending page, but I didn't
              get redirected to csi_search.php. Suggestions?


              Comment

              • Geoff Berrow

                #8
                Re: How to process text from HTML form?

                I noticed that Message-ID: <qPSdnQbbxscGHZ _cRVn-ug@comcast.com> from
                Chung Leong contained the following:
                [color=blue]
                >Don't forget to escape HTML special characters. Hence
                >
                ><input type="text" name="name"valu e="<?php echo
                >htmlspecialcha rs($_POST['search_field']); ?>">[/color]

                Nah, that's the wrong place for it. Besides, he may want to preserve
                markup.

                He might want to use stripslashes($_ POST['search_field']) though

                --
                Geoff Berrow (put thecat out to email)
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                • Geoff Berrow

                  #9
                  Re: How to process text from HTML form?

                  I noticed that Message-ID:
                  <55wMc.1564$AY5 .234@newssvr21. news.prodigy.co m> from deko contained the
                  following:
                  [color=blue]
                  >But the receiving form is on a different page - how do I pass the value of
                  >$_POST to this other page?[/color]

                  <form name="frmSearch " method="post" action="other_p age.php">

                  --
                  Geoff Berrow (put thecat out to email)
                  It's only Usenet, no one dies.
                  My opinions, not the committee's, mine.
                  Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                  Comment

                  • deko

                    #10
                    Re: How to process text from HTML form?

                    > <form name="frmSearch " method="post" action="other_p age.php">

                    I'll give it a shot....

                    Regarding cancelling action if a form is blank, I'm trying to get this to
                    work:

                    <form method="POST" action=
                    "<?
                    if ( empty($_POST['message']) || empty($_POST['email_address']) )
                    {
                    } //by the way, is there a better way to construct this without the else
                    statement?
                    else
                    {
                    include "send_mail.php" ;
                    }
                    ?>"
                    style="font-family: Arial, Helvetica, sans-serif; font-size:12px">
                    Email address: <input name="email_add ress" type="text" /><br />
                    Message:<br />
                    <textarea name="message" rows="12" cols="25"></textarea><br />
                    <input type="submit" value="Send" style="font-family: Arial, Helvetica,
                    sans-serif">
                    </form>

                    The problem is that get errors the second time I try ti use the form - but
                    it seems to work the first around. The error I get is something like:

                    Warning</b>:%20%20Cannot %20modify%20hea der%20informati on%20-%20headers%20al r
                    eady%20sent%20b y%20(output%20s tarted%20at%20/home/clearpoi/public_html/beta/
                    contact.php:19) %20in%20<b

                    My guess is that I need to reset the form after processing it. Should I
                    have some kind of reset action in send_mail.php? Below is the script. As
                    you will see, I am trying to send the user to a thank you page after
                    submitting feedback. This seems to be buttering things up somehow...

                    <?php
                    $email = $_REQUEST['email'] ;
                    $message = $_REQUEST['message'] ;
                    mail( "dave@Clearpoin tSystems.com", "CSI Feedback Form", $message, "From:
                    $email_address" );
                    header( "Location: http://www.ClearpointS ystems.com/beta/thankyou.html" );
                    ?>


                    Comment

                    • deko

                      #11
                      Re: How to process text from HTML form?

                      > <form name="frmSearch " method="post" action="other_p age.php">

                      That did the trick. Thanks.


                      Comment

                      • Geoff Berrow

                        #12
                        Re: How to process text from HTML form?

                        I noticed that Message-ID:
                        <%CxMc.795$H43. 297@newssvr27.n ews.prodigy.com > from deko contained the
                        following:
                        [color=blue]
                        >My guess is that I need to reset the form after processing it. Should I
                        >have some kind of reset action in send_mail.php? Below is the script. As
                        >you will see, I am trying to send the user to a thank you page after
                        >submitting feedback. This seems to be buttering things up somehow...
                        >
                        ><?php
                        >$email = $_REQUEST['email'] ;
                        >$message = $_REQUEST['message'] ;
                        >mail( "dave@Clearpoin tSystems.com", "CSI Feedback Form", $message, "From:
                        >$email_address " );
                        >header( "Location: http://www.ClearpointS ystems.com/beta/thankyou.html" );
                        >?>[/color]

                        Yes, but where is that in the script? If you have output html before
                        it, it will not work.

                        --
                        Geoff Berrow (put thecat out to email)
                        It's only Usenet, no one dies.
                        My opinions, not the committee's, mine.
                        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                        Comment

                        • deko

                          #13
                          Re: How to process text from HTML form?

                          > Yes, but where is that in the script? If you have output html before[color=blue]
                          > it, it will not work.[/color]

                          where is what? output html? before what? sorry, no comprenday...


                          Comment

                          • Geoff Berrow

                            #14
                            Re: How to process text from HTML form?

                            I noticed that Message-ID:
                            <5eyMc.801$ss3. 310@newssvr27.n ews.prodigy.com > from deko contained the
                            following:
                            [color=blue][color=green]
                            >> Yes, but where is that in the script? If you have output html before
                            >> it, it will not work.[/color]
                            >
                            >where is what? output html? before what? sorry, no comprenday...[/color]

                            Post the full script


                            --
                            Geoff Berrow (put thecat out to email)
                            It's only Usenet, no one dies.
                            My opinions, not the committee's, mine.
                            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                            Comment

                            • deko

                              #15
                              Re: How to process text from HTML form?

                              > Post the full script

                              Here's the whole enchlada. To duplicate the problem: go to
                              http://www.clearpointsystems.com/beta/contact.php and enter anything in the
                              Email Address form, and the Message form, and click send. You *should* be
                              taken to the "Thank You" page at
                              http://clearpointsystems.com/beta/thankyou.html. Now go back to contact.php
                              and click on the Send button *without entering anything* in either form.
                              You will receive a 404 - File not found error - with something that looks
                              like this in your browser's address bar:

                              Warning</b>:%20%20Cannot %20modify%20hea der%20informati on%20-%20headers%20al r
                              eady%20sent%20b y%20(output%20s tarted%20at%20/home/clearpoi/public_html/beta/
                              contact.php:19) %20in%20<b

                              Here is code from the Feedback form:

                              <div id="right_menu_ feedback">
                              <form method="POST" action=
                              "<?
                              if ( empty($_POST['message']) || empty($_POST['email_address']) )
                              {
                              }
                              else
                              {
                              include "send_mail.php" ;
                              }
                              ?>"
                              style="font-family: Arial, Helvetica, sans-serif; font-size:12px">
                              Email address: <input name="email_add ress" type="text" /><br />
                              Message:<br />
                              <textarea name="message" rows="12" cols="25"></textarea><br />
                              <input type="submit" value="Send" style="font-family: Arial, Helvetica,
                              sans-serif">
                              </form>
                              </div>

                              Here is the entire send_mail.php file:

                              <?php
                              $email = $_REQUEST['email'] ;
                              $message = $_REQUEST['message'] ;
                              mail( "myaddress@mydo main.com", "CSI Feedback Form", $message, "From:
                              $email_address" );
                              header( "Location: http://www.mydomain.co m/thankyou.html" );
                              ?>

                              Perhaps I shouldn't have a separate script to send the message - can I merge
                              send_mail.php into the form code on contact.php? Not sure this would solve
                              the problem... which I'm sure is something elementary...

                              Thanks again for your help.


                              Comment

                              Working...