Jump to content

HTML form action a PHP function


sanchez77

Recommended Posts

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

<?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.

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

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.

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]

Archived

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

×
×
  • Create New...

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.