sanchez77 Posted March 9, 2012 Share Posted March 9, 2012 How do you have an HTML form process a function for an action? I tried what I have below, but i just can't get it to work right. Any ideas? Thanks for your help. For example: <?php function UserLookup(){ $searchfield = $_POST['searchfield']; $client = new SoapClient("http://www.domain.com/webservice.asmx?WSDL"); $result = $client->UserLookup(array('searchfield'=>$searchfield)); echo json_encode($result); } if (isset($_GET['submit'])) { UserLookup(); } else { ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" > <input name="searchfield" type="text" size="45" /> <input type="submit" value=" Search " /> </form> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/258600-html-form-action-a-php-function/ Share on other sites More sharing options...
cpd Posted March 9, 2012 Share Posted March 9, 2012 <?php if (isset($_GET['submit'])) { $searchfield = $_POST['searchfield']; $client = new SoapClient("http://www.domain.com/webservice.asmx?WSDL"); $result = $client->UserLookup(array('searchfield'=>$searchfield)); } else { ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" > <input name="searchfield" type="text" size="45" /> <input type="submit" value=" Search " /> </form> <?php } ?> You'd be better of using the above code however... I've not had much experience with Soap but what your essentially trying to do is execute a function which, as far as I can see, does not exist within the SoapClient object. Refer to http://php.net/manual/en/class.soapclient.php for the available methods within a SoapClient object. A little more detail $client = new SoapClient("http://www.domain.com/webservice.asmx?WSDL"); This creates your object and assigns it to the variable $client allowing you to access the methods (functions) within the SoapClient object (described in the above URI) $client->UserLookup(array('searchfield'=>$searchfield)); Executes a method called "UserLookup" which is not defined inside the SoapClient object. You have defined it as a function above the if statement which tests for a form submission therefore, your executing a non-existent method in an object. Link to comment https://forums.phpfreaks.com/topic/258600-html-form-action-a-php-function/#findComment-1325603 Share on other sites More sharing options...
sanchez77 Posted March 9, 2012 Author Share Posted March 9, 2012 Thanks for your help, the SOAP function works fine when I call it outside of function tag. I am having issue executing the function. Using your example, I put the function code inside the if and then added echo "test"; to see if it would execute and never does that. Do I have the form setup right? Thanks, sanchez Link to comment https://forums.phpfreaks.com/topic/258600-html-form-action-a-php-function/#findComment-1325607 Share on other sites More sharing options...
cpd Posted March 9, 2012 Share Posted March 9, 2012 Thanks for your help, the SOAP function works fine when I call it outside of function tag. I am having issue executing the function. Using your example, I put the function code inside the if and then added echo "test"; to see if it would execute and never does that. Do I have the form setup right? Thanks, sanchez Yes you do but I've just noticed your trying to $_GET the data. Your form uses $_POST data, meaning it posts data to the server, therefore you should access it through the $_POST HTTP variable. I'm fairly certain it'll still fail though. Link to comment https://forums.phpfreaks.com/topic/258600-html-form-action-a-php-function/#findComment-1325609 Share on other sites More sharing options...
cpd Posted March 9, 2012 Share Posted March 9, 2012 Moreover, your going to try to access a piece of post data with a name "submit" however, if you look at your "submit" input tag you have not set a name. It should read as follows: <input type="submit" name="submit" value="SOME VALUE" /> Link to comment https://forums.phpfreaks.com/topic/258600-html-form-action-a-php-function/#findComment-1325610 Share on other sites More sharing options...
sanchez77 Posted March 9, 2012 Author Share Posted March 9, 2012 name of the submit button was what I was missing as well as the POST tag. Thank you for your help. It works. This is the code below: <?php function UserLookup(){ $searchfield = $_POST['searchfield']; $client = new SoapClient("http://www.domain.com/webservice.asmx?WSDL"); $result = $client->UserLookup(array('searchfield'=>$searchfield)); echo json_encode($result); } if (isset($_POST['submit'])) { UserLookup(); } else { ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" > <input name="searchfield" type="text" size="45" /> <input type="submit" name="submit" value=" Search " /> </form> <?php } ?> [\code] Link to comment https://forums.phpfreaks.com/topic/258600-html-form-action-a-php-function/#findComment-1325611 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.