Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Form without Action

    Trying to direct a form to another page based upon textfield value.

    Hoping to avoid using the "Action" command in the "Form" description. I want the code to figure out if the textfield "searchstring" is being utilized.
    The "Submit" button has an "onClick" command for function "resultChooser()".

    I don't see what I'm missing.

    Form Page:
    Code:
    <html>
    <head>
    <script src="javascript.js"></script>
    </head>
    
    <body>
    <form method="POST" name="form99">Search using partial criteria.
    <table>
    	<tr>
    		<td>Searching by:</td>
    		<td>Insert search criteria. (Leave blank to view all records.)</td>
    	</tr>
    	<tr>
    		<td>Course title</td>
    		<td><input type="text" name="searchstring"></td>
    	</tr>
    </table>
    <div style="position: absolute; left: 60px; top: 80px; width:255px;>
    	<input type="submit" value="Submit" name="B1" onClick="resultChooser()">
    	<input type="reset" value="Reset" name="B2">
    </div> 
    </form>
    </body>
    </html>

    Javascript page:
    Code:
    //Boolean search results chooser
    function resultChooser(){
    	if (document.form99.searchstring != '')
    	{
    		window.location="searchBool.php";
    	} else {
    		window.location="searchBool_all.php";
    	}
    }
    Using Firefox Firebug plugin for testing.
    It doesn't show any errors. And it confirms that the called function, located in the javascript.js page, is being reached.

    Presently, the page only reloads itself and clears any data in the textfield.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Is this what you wanted?
    Code:
    function resultChooser(frm){
    	if (frm.searchstring != '')
    	{
    		frm.action="searchBool.php";
    	} else {
    		frm.action="searchBool_all.php";
    	}
    }
    Code:
    <form method="POST" name="form99" onsubmit="resultChooser(this);">
    ...
    <input type="text" name="searchstring">
    ...
    <input type="submit" value="Submit" name="B1">
    <input type="reset" value="Reset" name="B2">
    </form>
    But why don't you have a single php page and do the logic there? The only difference between the 2 searches is that "search all" won't have a "where" clause in the SQL query while the other will have.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's got it going. Thank you.
    Forgot about the "onSubmit" command.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You can also do it without the onsubmit handler. You can do it with the onclick in the submit button as you were doing. The main solution is setting the form action to appropriate php page.

    But as I also said, you can have a single php action page and avoid this javascript code. You would put the logic in the php page. In the php page you can determine if the "searchstring" is empty or not and create the appropriate SQL query based on it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •