Jump to content

Archived

This topic is now archived and is closed to further replies.

Jocka

checking forms and submiting correctly

Recommended Posts

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:
[code]
<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>
[/code]
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
So you can't check the statements and redirect the page according to what input fields were used?

Share this post


Link to post
Share on other sites
"Check the statements"?  And sure, you can do dynamic form actions.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sure, there's no reason why you can "validate" in JS and then redirect.

Share this post


Link to post
Share on other sites
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:
[code]
<?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");

?>
[/code]

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.

Share this post


Link to post
Share on other sites
Sure... (untested):

[code]
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('&');

}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.