set text box value to variable returned from function..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danturn
Forum Newbie
Posts: 17
Joined: Wed Sep 02, 2009 10:23 am

set text box value to variable returned from function..

Post by danturn »

Hey guys,

I need to update a text box on my HTML form with a variable returned from a regex search in a function...

I can echo out the value within the function which works fine and contains the right info...

just cant work out how to set the text box to that value...

heres my code:

Its the MAC text box i want to populate with either the $mac[1] or $macadd...

Code: Select all

$form = 
"
<body>
<form method='post' action='aaa.php'> 
<div id='Info'>
<input name= 'MAC' type= 'text' value= '$macadd'>
<input type= 'submit' name= 'info' value='info'>
</div>
</body>
";
    
    if (isset($_POST['info']))
    {
        $xml = "";
        $method = "GET";
        $posttxt = "/DeviceInformation";
        sendXML($xml,$posttxt,$method);
    }
 
    function sendXML($xml,$posttxt,$method)
    {
        $ip = $_POST['IP'];
        $userid = $_POST['USERID'];
        $pin = $_POST['PIN'];
        $auth = base64_encode($userid.":".$pin);
        $xml = "XML=".urlencode($xml);
        $post = $method." ". $posttxt. " HTTP/1.0\r\n";
        $post.="Host: $ip\r\n";
        $post.="Authorization: Basic $auth\r\n";
        $post.="Connection: close\r\n";
        $post.="Content-Type: application/x-www-form-urlencoded\r\n";
        $post.="Content-Length: ".strlen($xml)."\r\n\r\n";
        $response = "";
        $sock = fsockopen($ip, 80, $errno, $errstr, 30);
        
        if ($sock) 
        {
            fwrite($sock, $post . $xml);
            while (!feof($sock)) 
            {
            $response .= fgets($sock, 128);
            }
            fclose($sock);
        }
        
        if (strpos($response, 'Status="0"') !== false) 
        {
            echo 'Authenticated<BR>';
        }
            
        
    if(preg_match(
        '@MAC Address</B></TD><td width=20></TD><TD><B>(.*?)</B>@',$response,$mac
        )) 
    {
    $macadd=$mac[1];
    }
    else{echo $response;}
    }
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: set text box value to variable returned from function..

Post by cpetercarter »

Couple of suggestions :

1 put the div tags outside the form, not inside
2 close the input tags properly (eg <input name= 'MAC' type= 'text' value= '$macadd' />
3 close the form </form>
danturn
Forum Newbie
Posts: 17
Joined: Wed Sep 02, 2009 10:23 am

Re: set text box value to variable returned from function..

Post by danturn »

Hey Cpetercarter,

Thanks for that... i've closed my input tags properly now and closed the form...

i dont get how i'd put the div tags outside the form... im breaking sections of the form down into sections and using divs and css to position them on the page...?
cpetercarter wrote:Couple of suggestions :

1 put the div tags outside the form, not inside
2 close the input tags properly (eg <input name= 'MAC' type= 'text' value= '$macadd' />
3 close the form </form>
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: set text box value to variable returned from function..

Post by cpetercarter »

You can, of course, style the form with css, but it is often easiest to use a table to position the form elements nicely.
danturn
Forum Newbie
Posts: 17
Joined: Wed Sep 02, 2009 10:23 am

Re: set text box value to variable returned from function..

Post by danturn »

i did try initially with tables but i couldnt get the positioning i needed accurately enough.

i'm making a remote for Cisco IP phone so positioning the buttons etc. is a bit fiddly!

Any advice about filling my text boxes as I'm still struggling :*(

Dan
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: set text box value to variable returned from function..

Post by cpetercarter »

Sorry. Forgot your original problem!

I think the problem is that you are trying to make a variable $form with the html for the form inside it. If you do this, it will incorporate $macadd as a literal. It won't take the value that $macadd happens to have at the time you create the variable. You could try this

Code: Select all

 
 $form =
 "
<body>
<form method='post' action='aaa.php'>
<input name= 'MAC' type='text' value='".$macadd." />
<input type='submit' name= 'info' value='info' />
</form>
</body>
";
 
You can give the various form elements ids and use those to style with, if it helps.
danturn
Forum Newbie
Posts: 17
Joined: Wed Sep 02, 2009 10:23 am

Re: set text box value to variable returned from function..

Post by danturn »

Thanks for that dude,

I actually got it working by moving the form="HTML CODE HERE" bit from the top of the page to the bottom.

god knows why it works now but it does!

Thanks for your help though

Dan
Post Reply