Page 1 of 1

set text box value to variable returned from function..

Posted: Wed Sep 09, 2009 3:38 am
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;}
    }

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

Posted: Wed Sep 09, 2009 8:46 am
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>

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

Posted: Wed Sep 09, 2009 9:28 am
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>

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

Posted: Wed Sep 09, 2009 1:08 pm
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.

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

Posted: Thu Sep 10, 2009 3:14 am
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

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

Posted: Thu Sep 10, 2009 8:27 am
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.

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

Posted: Fri Sep 11, 2009 4:42 am
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