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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.