Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Form with cookies - redirects if cookies exist

New Here ,
Feb 14, 2012 Feb 14, 2012

Hi everyone. I am trying to code a form that contains cookies and redirects.

This is what I want to do:

  • There is a link to a pdf.
  • If the person clicks the link, the form shows up and they fill out info. Then they redirect to the pdf.
  • If the person views another pdf or even the same pdf later, I want to skip the form page and simply go to the pdf.

I have the cookies working for the form, but I have no idea how to make this redirect work. Any ideas or help? Thanks!

The form and cookie code:

<form name="form_test" id="form_test">

      <table>

  <tr>

          <td class="FormHeader" align="right">Name:</td>

          <td align="left"><input type="text" name="Name" size="40" maxlength="50"></td>

</tr>

<tr>

          <td class="FormHeader" align="right">Company:</td>

          <td align="left"><input type="text" name="Company" size="40" maxlength="50"></td>

</tr>

<tr>

          <td class="FormHeader" align="right">Address:</td>

          <td align="left"><input type="text" name="Address" size="40" maxlength="50"></td>

</tr>

<tr>

          <td class="FormHeader" align="right">Address 2:</td>

          <td align="left"><input type="text" name="Address2" size="40" maxlength="50"></td>

</tr>

<tr>

          <td class="FormHeader" align="right">City:</td>

          <td align="left"><input type="text" name="City" size="40" maxlength="50"></td>

</tr>

<tr>

          <td class="FormHeader" align="right">State:</td>

          <td align="left"><input type="text" name="State" size="40" maxlength="50"></td>

</tr>

<tr>

          <td class="FormHeader" align="right">Country:</td>

          <td align="left"><input type="text" name="Country" size="40" maxlength="50"></td>

</tr>

<tr>

          <td class="FormHeader" align="right">Zip:</td>

          <td align="left"><input type="text" name="Zip" size="10" maxlength="50"></td>

</tr>

<tr>

          <td class="FormHeader" align="right">Phone:</td>

          <td class="FormHeader" align="left">

                    <input type="text" name="Phone" size="15" maxlength="50">

                    Fax: <input type="text" name="Fax" size="15" maxlength="50">

          </td>

</tr>

<tr>

          <td class="FormHeader" align="right">E-Mail:</td>

          <td align="left"><input type="text" name="Email" size="40" maxlength="50"></td>

</tr>

<tr>

          <td align="right"><input type="checkbox" name="CallMe" value="Have a sales person call me" checked></td>

          <td class="FormHeader" align="left">Have a salesperson call me</td>

</tr>

<tr>

          <td align="right"><input type="checkbox" name="Catalog" value="Send me a catalog"></td>

          <td class="FormHeader" align="left">Send me a catalog</td>

</tr>

<tr>

          <td class="FormHeader" align="right">Comments:</td>

          <td align="left"><textarea cols="40" rows="3" name="Comments"></textarea></td>

</tr>

<tr>

          <td align="center" colspan="2"><br><input type="submit" name="submit" value="Submit"></td>

</tr>

</table>

</form>

<script language="javascript">

function setCookie( name, value, expires, path, domain, secure ) {

    var today = new Date();

    today.setTime( today.getTime() );

    if ( expires ) {

        expires = expires * 1000 * 60 * 60 * 24;

    }

    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name+"="+escape( value ) +

        ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) +

        ( ( path ) ? ";path=" + path : "" ) +

        ( ( domain ) ? ";domain=" + domain : "" ) +

        ( ( secure ) ? ";secure" : "" );

}

</script>

<script language="javascript">

function getCookie( name ) {

    var start = document.cookie.indexOf( name + "=" );

    var len = start + name.length + 1;

    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {

        return null;

    }

    if ( start == -1 ) return null;

    var end = document.cookie.indexOf( ";", len );

    if ( end == -1 ) end = document.cookie.length;

    return unescape( document.cookie.substring( len, end ) );

}

</script>

<script language="javascript">

function rememberFormInputs(form_id, prefix) {

    // get a reference to the form element with id 'form_test'

    var form = document.getElementById(form_id);

    // get all child input elements of the form

    var els = document.getElementsByTagName('input');

    // iterate through all form child input elements

    for (var i = 0; i < els.length; i++) {

        // this is the element with index of i

        var el = els.item(i);

        // make sure this is a text input field

        if (el.type == 'text') {

            // event handler to catch onblur events

            // it sets the cookie values each time you move out of an input field

            el.onblur = function() {

                // this is the name of the input field

                var name = this.name;

                // this is the value of the input field

                var value = this.value;

                // set the cookie

                setCookie( prefix + name, value);

                alert('setCookie: '+name + ' = '+value);

            };

            // this inserts all the remembered cookie values into the input fields

            var old_value = getCookie(prefix + el.name);

            if (old_value && old_value != '') {

                alert('old value remembered: '+old_value);

                el.value = old_value;

            }

        }

    }

}

</script>

<script language="javascript">

window.onload = function() {

    rememberFormInputs('form_test', 'input-');

}

function alert(str) {

    var el = document.getElementById('alert');

    if (el) {

        el.value += str+"\r\n";

    }

}

</script>

TOPICS
Server side applications
519
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 14, 2012 Feb 14, 2012
LATEST

Please don't post duplicate questions. You asked the exact same question a few days ago which was answered. If you didn't understand the answer, then reply back in that thread. Thanks,.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines