Is there another way to handle this?


#1

OK i have a form automatically being submitted by javascript. The problem - People turn off javascript. So can this be done with PHP with the header function (I have never used it before)? I also tried it with the HTML meta refresh tag and that didn’t work. Anyone have any other ideas for how to submit the form automatically? Or am I just SOL and just let the user hit the back button?

if ($error)
{
    // OR RESET STEPCOMPLETE AND RELOAD PAGE
    echo "<CENTER><FONT COLOR = "RED" SIZE = "+2">Error: Input not an acceptable table</FONT></CENTER>";
    ?>
    <FORM METHOD = "post" ACTION = "qualifiers.php" Name = "stepBack">
    <INPUT TYPE = "hidden" NAME = "stepComplete" VALUE = "">
    </FORM>
    <!-- SUBMITS FORM AUTOMATICALLY -->
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    setTimeout('document.stepBack.submit()',1000);
    //-->
    </SCRIPT>
    <?php
    exit;
}

#2

you cannot post data with the meta refresh tag nor can you do it with header(), i suggest just using a submit button.


#3

You can always get your page to generate a POST request and fetch the page from the server, and display the result. If you are using php >4.3, you can use the stream to handle the request (see stream_context_create(), there is a great article in PHP|Architect in the May issue). Otherwise, you will need to depend on cURL, which is an external library and may not be available everywhere.


#4

Thanks Bane. Have read parts of the May issue - Shifletts corner (always makes me paranoid which is why I am trying to make the site tighter) as well as “accessable websites” and struggling through George’s article on regex - so I guess I should also take a closer look at the cURL article. sigh just when I think I am getting a grip on this stuff - I find out just how much I don’t know… :)


#5

OK Bane this is what I have and it still isn’t reloading

/* HANDLE SUBMIT WITH JAVASCRIPT AND HTML 
THIS WORKS BUT WHAT ABOUT IF JAVASCRIPT IS TURNED OFF?
<FORM METHOD = "post" ACTION = "qualifiers.php" Name = "stepBack">
<INPUT TYPE = "hidden" NAME = "stepComplete" VALUE = "">
</FORM>
<!-- SUBMITS FORM AUTOMATICALLY -->
<SCRIPT LANGUAGE="JavaScript">
<!--
setTimeout('document.stepBack.submit()',1000);
 //-->
</SCRIPT>
<?php
*/

/* HANDLE SUBMIT WITH PHP*/
// Here is where I thought I put my POST variables
$data = "stepComplete=";
echo "$data<br>";

// use http protocal with POST method and data
$opt = array('http' => array('method' => 'POST' ,
                             'header' => "Content-Type: application/x-www-form-urlencodedrn" .
                                         "Content-Length: ".strlen($data), 
                             'content' => $data));

// create the stream
$context = stream_context_create($opt);

// and reopen my page
$fp = fopen("http://localhost/".$_SERVER['PHP_SELF'], "r", false, $context);
if ($fp)
{
    echo "Successful connection to page";
}
 else
{
    echo "Go suck an egg";
}

well it connects to the page (I think - no errors) but all get is a blank page instead of displaying my default case page (Switch statement on stepComplete). I thought that the fopen would reload the URL. I even get

stepComplete=
Successful connection to page

So I guess my question is reletively simple… where did my logic and what actually happens take seperate paths? Or am I not going far enough? I know 0 about the HTTP protocol (though I will be researching it after I post this).


#6

The JS part shouldn’t be required. Seems like the operation succeeded, you simply don’t handle the post submit in the script.

Also, take care with recursive calls in a script.


#7

The JS part actually is commented out. How do I handle the POST afterward. There is nothing in the article and the HTTP RFC leaves me dazed and confused. The manual says nothing about how to proceed and I haven’t been able to find a tutorial on the subject anywhere. My books don’t cover the subject either so I have no idea what to do next.

I was thinking an fread or a context_get_contents but that isn’t really what I want to do.

Any ideas or hints for the next step would be appreciated.


#8

Well, the PHP script will store the variables from the post request in $_POST… If you don’t do anything about those, you don’t really handle the post request. In order to make a test, have two separate scripts (one to play with streams and one to test on), otherwise, it will only be a huge mess.


#9

Hmmm… I thought I was handling it with this

if (isset($_POST['stepComplete']))
{
    $_SESSION['stepComplete'] = $_POST['stepComplete'];
}

I currently am using a work around with JS and Session variables. If the JS is off the user has to hit the back button. Not what I want but shrugs it will work.

I guess I need to play with it some more to get the hang of it. Also found out the server supports cURL so I guess I should look into that (thought there is no guarentee the next hosting server will).

So much to learn so little time and so much to rewrite once you learn something new.