Jump to content


Photo

checking forms and submiting correctly


  • Please log in to reply
7 replies to this topic

#1 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 10 October 2006 - 05:08 PM

I made a post in the mod_rewrite forum before realizing that it's not mod_rewrite I should be using. It's JAVASCRIPT.

What I need this code to do is check for any non-empty input fields and submit (to the same page) these fields ONLY.

Example:
<html>
<head>

// JAVASCRIPT STUFF HERE (i dont' know it) //
</head>

<body>
<form name="search" method="GET" onsubmit="check_form()">
<input type="text" name="user" value=""><br>
<input type="text" name="city" value=""><br>
<input type="text" name="state" value=""><br>
<input type="submit" name="submit" value="Search">
</form>
</body>
</html>
then it runs the code so that the url after submitting isn't like: search.php?user=&city=Dallas&state=
I just want it to say (in this case): search.php?city=Dallas

Right now i'm using another small PHP file to do this but I'd rather have it do all this on the same page.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 October 2006 - 07:25 PM

I don't really know why you care... and you "can't", strictly speaking, according to the GET method.  I guess you could disable these inputs via JS and then process the form.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 10 October 2006 - 09:24 PM

So you can't check the statements and redirect the page according to what input fields were used?

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 October 2006 - 10:12 PM

"Check the statements"?  And sure, you can do dynamic form actions.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 11 October 2006 - 04:34 PM

By "Check the statements" I mean, check the fields for ones that aren't empty.. .. I just was lazy and didn't want to type all that out.

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 11 October 2006 - 10:59 PM

Sure, there's no reason why you can "validate" in JS and then redirect.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 12 October 2006 - 01:07 AM

Well I know I can. What I need is some code to get me started. I used form validation codes I found online but they didn't help any. Basically I wanted it to do something like this:

PHP WAY OF DOING IT:
<?php

$link = NULL; // TO SAVE QUERY INFO

foreach($_GET as $key => $value){

  if($value !== '' && $key !== 'submit'){

    if($link == NULL){

      $link .= "?" . $key . "=" . $value;

    } else {

      $link .= "&" . $key . "=" . $value;

    }

   }

}

$link = "search.php" . $link;

header("Location: $link");

?>

Thats what I use now for the PHP file that does what I want. But I need a JAVASCRIPT version of this that does it as soon as the page is submitted.

#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 12 October 2006 - 01:41 AM

Sure... (untested):

function check_form() {
var links = [];
for( var el in document.forms['search'].elements ){
  if( el.type == 'text' && el.value != '' el.name != 'submit'){
    links.push( el.name . '=' . el.value );
  }
}

window.location = 'search.php?' . links.join('&');

}

Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users