Jocka Posted October 10, 2006 Share Posted October 10, 2006 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=DallasRight now i'm using another small PHP file to do this but I'd rather have it do all this on the same page. Quote Link to comment Share on other sites More sharing options...
fenway Posted October 10, 2006 Share Posted October 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
Jocka Posted October 10, 2006 Author Share Posted October 10, 2006 So you can't check the statements and redirect the page according to what input fields were used? Quote Link to comment Share on other sites More sharing options...
fenway Posted October 10, 2006 Share Posted October 10, 2006 "Check the statements"? And sure, you can do dynamic form actions. Quote Link to comment Share on other sites More sharing options...
Jocka Posted October 11, 2006 Author Share Posted October 11, 2006 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. Quote Link to comment Share on other sites More sharing options...
fenway Posted October 11, 2006 Share Posted October 11, 2006 Sure, there's no reason why you can "validate" in JS and then redirect. Quote Link to comment Share on other sites More sharing options...
Jocka Posted October 12, 2006 Author Share Posted October 12, 2006 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 INFOforeach($_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. Quote Link to comment Share on other sites More sharing options...
fenway Posted October 12, 2006 Share Posted October 12, 2006 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.