Member Avatar for FakeTales

I am able to set up a connection via client to server , however i am trying to write to a textfile the variables from a form this form is then sent to the server it is opened up then i exploded the data within the text file , place these exploded items into a string then post this string back to the client. I can achieve this however the problem is that you have to click submit twice on the client side form for the changes to take place .

Server Code

<?php
// set some variables
$host = "127.0.0.1";
$port = 9100;
// don't timeout!
set_time_limit(0);
do{
echo $host;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");

// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");

while (true) {
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");

// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string

if (trim($input) == "END") { 
socket_close($spawn); break;
}


$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$stringData = fread($fh, filesize($myFile));
fclose($fh);

$input = $stringData;
$output = $input;

if($output !=null){

    $items = explode(",", $output);

    echo $items [0];
    echo $items [1];
    echo $items [2];

    }



$output = "<p> <h3 style=".'"color:'.$items[1].'"'."> Hello ".$items[0]."</h3> </p>";

socket_write($socket, $str);
socket_write($spawn, $output, strlen($output)) or die("Could not write output\n");

} 
socket_close($socket);
Socket_close($spawn);
}while ($input != "END");
?>

Client Code

<html>
    <head>
    <link href="/style.css" rel="stylesheet" type="text/css"/>

    </head>

    <body>

        <div id="header">
            <?php
            // Server brings back info from the header text box 
                if(isset($_POST['submit'])) {
                    $str = $_POST["hdr"].",".$_POST["hdrCol"].",".$_POST["backCol"];
                        do_it($str);

                    }
            ?>
        </div>

        <div id="bodyCont">

            <div id="bodyLeft">

            <form
                action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
                    <label> Header: </label>
                    <input type="text" name="hdr" value="" >
                </br>
                </br>
                <label> Header Text Colour: </label>
                <select name="hdrCol" >
                <option name="hdrCol" value=" Red "> Red </option>
                <option name="hdrCol" value=" Blue "> Blue </option>
                <option name="hdrCol" value=" Orange "> Orange </option>
                <option name="hdrCol" value=" Yellow "> Yellow </option>
                </select>
                </br>
                </br>
                <label> Header Background Colour: </label>
                <select name="backCol" id="backCol">
                <option name="backCol" value=" Red "> Red </option>
                <option name="backCol" value=" Blue "> Blue </option>
                <option name="backCol" value=" Orange "> Orange </option>
                <option name="backCol" value=" Yellow "> Yellow </option>

                <input type="submit" name="submit" value="Submit Style">
            <?php

            // write variable from text box
            $myFile = "testFile.txt";
            fwrite($fh, $stringData);
            $fh = fopen($myFile, 'w') or die("can't open file");
            $stringData = $str;
            fwrite($fh, $stringData);
            fclose($fh);
            die();
            ?>
                </form>


            </div>   


        </div>

        <?php
function do_it($str)
{
// set some variables
$host = "127.0.0.1";
$port = 9100;
$ret;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$ret = socket_connect($socket, $host, $port);

socket_write($socket, $str);

$input = socket_read($socket,1024);
$input = trim($input);
echo $input;
socket_close($socket);
}

?>
    </body>

</html>

Summary - I am able to post the data from the form to the server , the server then posts back only if the client side submit button is pressed twice. I need the information to come back on one click

Recommended Answers

All 2 Replies

Member Avatar for LastMitch

@tamblyn

I can achieve this however the problem is that you have to click submit twice on the client side form for the changes to take place .

The issue is with your <form>. I don't see anything wrong from your php code that is shown an error because you don't have a foreach loop issue. You need to fixed your <form> then it will be fine.

Member Avatar for FakeTales

After a few hours of swearing at it , i have got it working , i moved the text file write to under the </form> on the client side and i also i removed " $input = $stringData; "
on the server side

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.