Passing conditional string to subsequent web page

I am constructing a Mandarin tuition intranet site which will include interactive translation exercises. To maximise the spread of vocabulary for practising a particular sentence pattern, I have designed a system in which sentence components are picked randomly from a MySql table, but need to be able to rank the components so that only sensible combinations are assembled. A ‘hierarchy’ field in the table contains integers which form the basis of a test to distinguish valid and non-valid combinations. To this end I am experimenting with an intermediate page which onLoad POSTs a conditional string (extracted from a second table in the MySql database) to be used in a MySql retrieval process in the receiving page which should loop until a sensible sentence has been constructed. The string is of the following form:

[php](({$dd_test_val[0][1]}-{$dd_test_val[0][4]}==0) || ({$dd_test_val[0][1]}-{$dd_test_val[0][4]}==1)) & (({$dd_test_val[0][4]}-{$dd_test_val[0][2]}==0) || ({$dd_test_val[0][4]}-{$dd_test_val[0][2]}==1)) & ({$dd_test_val[0][1]}-{$dd_test_val[0][3]} < 4)[/php]

When it reaches the receiving page, the POSTed string is read into a variable $h_form, but simply creating the $dd_test_val array and testing if($h_form) doesn’t seem to work. Is there in fact any way of passing a conditional string in the way I am attempting, or is this potential solution doomed to failure?

Couple of ways that i know of doing this. If the string is going to be constant, then you can put it in the form action, such as processor.php?condition = 1. you can also use session variables to make it more dynamic. There’s also an attribute for the form tag that’ll do that too, encypt i think it is.

I would go with the $_SESSION variables. You can basically put whatever you wish into them with some limits. Therefore, they will hold most any variable.

But, now that that was said. It is usually not a good idea to call the current page over and over. This can cause all kinds of problems on the browser side. Some browsers crash if overloaded with recursion.

It would be better to handle that call in the PHP script on a separate page. Call out with the initial call and let it do it’s thing re-cursing as it needs to and then returning the final value. If you do not want the current screen to “refresh” when the results comes back, you can use a hidden iFrame and load it with the second PHP page. In this way the PHP recursive code is still done SERVER-SIDE and the results is posted back to the hidden page which can then be displayed. (That may be a bit hard to understand, but, it works well.)

For General info:
Also, you may be interested in something I was working with in the past. It is called “BNF” If you google “what is bnf” you will find a lot of interest in it. Also, there is a “PHP BNF CONVERTER” that I have seen online that is code to sort thru BNF automatically. “BNF” is now being heavily used in a different format called “AIML”. AIML is actually call “Artificial Intelligence Markup Language”. It is used for chatbot’s, but I think it may work for you. Is basically a XML file that can be put into a database as text and can be recursively scanned to make sense out of what someone types in. Tons of info on this online.
Sorry for the aside, but, who knows it might be interesting to you… Good luck…

Consider using markov chains to produce sensible sentences. Although this may take awhile to build a database, it works a treat, example google predictive search uses a markov chain system :slight_smile:

Thanks to all who have responded to my enquiry. To clarify my problem in the light of the advice so far in this thread:

  1. I feel that the Markov chain approach to this problem would be a sledgehammer to crack a nut. The sentence templates I am working with are very simple, and the vocabulary relatively easy to tag with explicit numerical values which operate reliably within the particular template. Each template will have its own associated conditional test.

  2. My intention has been to carry out the testing during the creation of what I refer to as the receiving page, using server-side php to apply the conditional test until a valid sentence has been built, setting an initial flag to false and switching it to true when the first valid combination of choices has been achieved, and then proceeding to write the rest of the page.

  3. There is no problem in passing the conditional test as a string variable using POST and reading it into a variable in the receiving page in the normal manner with $_POST[’’]. Echoing the variable shows that this has been done (Indeed, the string in my original enquiry was a cut-and-paste of the on-screen result of the echo.)

  4. The real problem appears to be in getting the retrieved string to function as a conditional test. So far, all my attempts (with a range of syntactic variations) for example if($h_form) { echo ‘TRUE’ } else { echo ‘FALSE’} result in the outcome ‘TRUE’, presumably because the variable $h_form exists, rather than because it has evaluated to a particular value.

  5. I appreciate that the original (real) conditional string was somewhat complicated, so a simple test has this conditional string in the starting php page:

[php]$h_var=’$test_var==4’;[/php]

which is POSTed as the value of a hidden form field named h_form and processed in the receiving page by the code:

[php]$test_var=5;
$testing=$_POST[‘h_form’];
echo"

Formula is: $testing

";
if($testing==true) { // I tried using the == true after finding that if($testing) alone didn’t work
echo “test passed”;
} else {
echo “test failed”;
}[/php]

The first echo line produces the string

Formula is: 5==4
so the substitution of value of the variable $test_var in the imported test string has occurred, but the second echo is ‘test passed’, although clearly $test_var was set to yield the result ‘test failed’.
  1. So, in this simple test, do I need to modify the exact syntax of the definition of $h_var in the starting php page, or do I need to use a more complex syntax in the receiving page, in order to achieve a ‘working’ conditional string in the latter? I have played with eval(), but again have not succeeded in getting the result I require.

I seem to have made a first step towards solving this one. If eval is used (correctly!) with the ‘return’ parameter, like this:
[php]$result = eval(“return (”.$testing.");");[/php]
in the receiving page with the simplified test I posted earlier, the logic works correctly. Theoretically this should extrapolate to the more complex conditional string with which I started, but I shall need to stitch the new code into the full sentence-building routine for comprehensive testing.

BTW, I assume that because the input to the eval command is retrieved within the site from data in a database which is behind a firewall, the use of eval is fairly safe. Anybody got any views on that?

PeterN

why not just use sessions?

Well, EVAL() is not allowed anymore. You can use it, but, it allows for code to be entered that can break your site. I think it is just something inside of the posting process. EVAL() has been deprecated and will eventually disappear. I see no way to do this by passing PHP variables. A string variable converted into code will open doors for bad code to show up. If you are the only one who is entering the data and it will never be altered by outside users, use EVAL() until it is gone. Hmmm? One other thing, a string passed to PHP IS a string, not PHP code. The only way I know how to evaluate a string into a compare would be to parse the string.

In your sample, you set a variable call $h_var and read it as $h_FORM… Could you strip down the two pages to the simplest number of lines, the php and the html which calls it and post them? Maybe we can figure it out that way.

Thanks, ErnieAlex,

First, to clarify, in the eventual implementation, the eval() will only ever get its argument from data which I have previously put into a securely located MySQL database, so I think your security concerns are met.

The code of the test starting page is:

[php]<?php
$formula_string=’$test_var==4’;
?>

Starting page

Enter test digit and click button to test logic: <?php echo"";?> [/php] and the code of the receiving page (receiver_test_dev.php) is:

[php]

Receiving page <?php $test_var=$_POST['digit_val']; $testing=$_POST['h_form']; echo"

Digit value is: $test_var

"; echo"

Formula is: $testing

"; $result = eval("return (".$testing.");"); echo"

Evaluation of result is: '$result'

"; if($result) { echo "test passed"; } else { echo "test failed"; } ?> [/php] This works in my set-up, and was designed specifically to test the use of eval(), but in devising an alternative strategy, it will need to handle a 'real-life' conditional along the lines of the example I gave earlier which tests against array contents available in the receiving page:

[php](({$dd_test_val[0][1]}-{$dd_test_val[0][4]}==0) || ({$dd_test_val[0][1]}-{$dd_test_val[0][4]}==1)) & (({$dd_test_val[0][4]}-{$dd_test_val[0][2]}==0) || ({$dd_test_val[0][4]}-{$dd_test_val[0][2]}==1)) & ({$dd_test_val[0][1]}-{$dd_test_val[0][3]} < 4)[/php]

I await your reaction with interest.

PeterN

PeterN,
Yes, if USERS of your site can not get data into the EVAL(), then it is safe. There are routines all over the net to pre-parse EVAL() data to make sure it is safe. But, in your case, it should be fine.
Also, I reviewed the PHP.net site and could not find any info about the actual “removal” of EVAL()… It appears they have just dropped it from their safe-code list… Not sure what that means. Here is a link to their definition. In the “TIP” near the bottom I found a note where you can capture output to a page from PHP with Output-Control-Functions. That was interesting, although not useful in your case. http://us2.php.net/manual/en/function.eval.php
One more note that was on several of the pages I viewed, stated that you can not capture errors while using EVAL(), so you have to test your input to it beforehand. This is not important for a small input such as yours.

So, now that I am off topic, yes, I would just use the EVAL() making sure you do not let others input data into it. Good luck… If you get it all working let us know… Or, post further questions…

Hi, ErnieAlex

Thanks for the latest. I have in fact now succeeded with the passing of the long conditional (subject to the removal of the superfluous curly brackets which it contained) so that the code of my ‘real-life’ receiving page provides a check for the building of an example sentence pattern with four separate and randomly filled multi-choice sections, thus ensuring that only sensible sentences can be presented to students in the rendered web page.

Slightly off-topic here, the only problem I found was unrelated to the passing of the conditional string, but arose from the way php handles its arrays, which I found unexpectedly cumulative. My real-life receiving page creates several arrays by assembling strings of values and then exploding them. Going back into my validation ‘while’ loop, which included repeating the ‘explosions’, didn’t over-write the previous set of stored values with the new random selection of sentence components, so if the first application of the condition resulted in failure, the code then went into endless looping since no succesful application of the test could occur. The simple step of explicitly unsetting the arrays after detection of failure of the condition cured this problem, and all is now singing and dancing.

Many thanks for your encouragement to an oldie newbie - who is also mightily relieved that eval() seems to be safe for the immediate future (and safe in another sense in this application)!

PeterN

Always GREAT to hear a project is solved!

Sponsor our Newsletter | Privacy Policy | Terms of Service