Jump to content

Need help accessing POST data


BenGoldberg

Recommended Posts

So basically I'm using PHP to solve a math problem for me. The user puts in a few parameters and the program runs those numbers through the algorithm and spits out a bunch of xy coordinates. I'm using the PHP SELF method to retrieve the user input. This all works fine, but it's afterwords that I run into problems. I create a drop down menu of all the x values and I want the user to be able to choose any x value they wish and to have the corresponding y value be displayed. The problem is that I have to use another POST command for this and when I do, it wipes out all the computed data.  Of course the y-value is never displayed, and there lies the problem.

 

I'm sure there are many ways to get around this, but I could not find one out myself. Anyone have any ideas?

Link to comment
Share on other sites

Alright, here's the important stuff. Don't get too caught up in the math, the important thing to know is my $a array are my x values and my $b array are my y values. I created the drop down menu of the x values at the end but there is no attempt on there of trying to display the y values. But basically if they choose $a[37] I would like $b[37] to be displayed.

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<?php

function dydx($x,$y){

$equation = 3 * pow($x, 2);
return $equation;
}

?>

<table>
<tr>
	<td>Enter number of partitions:</td>
	<td><input type="text" size="7" maxlength="7" name="partitions" value="" /></td>
</tr>
<tr>
	<td>Enter initial x value, x(0):</td>
	<td><input type="text" size="3" maxlength="3" name="initialx" value="" /></td>
</tr>
<tr>
	<td>Enter the ending x value:</td>
	<td><input type="text" size="3" maxlength="3" name="endingx" value="" /></td>
</tr>
<tr>
	<td>Enter initial y value, y(0):</td>
	<td><input type="text" size="3" maxlength="3" name="initialy" value="" /></td>
</tr>
</table>

</br>

<input type="submit" name="runge kutta name" value="Run Program" />

</form>

<?php

$partitions = $_POST['partitions'];
$initialx = $_POST['initialx'];
$endingx = $_POST['endingx'];
$initialy = $_POST['initialy'];

if ($_POST['partitions'] != 0) {

$h = ($endingx - $initialx)/$partitions;
}


$a[1] = $initialx;
$b[1] = $initialy;

for ($i = 1; $i <= $partitions; $i++) {

$k1 = dydx($a[$i], $b[$i]);
$k2 = dydx($a[$i] + (1/2)*$h, $b[$i] + (1/2)*$h*$k1);
$k3 = dydx($a[$i] + (1/2)*$h, $b[$i] + (1/2)*$h*$k2);
$k4 = dydx($a[$i] + $h, $b[$i] + $h*$k3);

$a[$i + 1] = $a[$i] + $h;
$b[$i + 1] = $b[$i] + (1/6)*$h*($k1 + 2*$k2 + 2*$k3 + $k4);
}

for ($i = 1; $i <= $partitions + 1; $i++) {

$xyarray["$a[$i]"] = $b[$i];
}

$_SESSION['graph'] = $xyarray;

?>

</br></br>

Choose a partition of X:



<select name="output">

<?php

for ($i = 1; $i <= $partitions + 1; $i++) {

echo "<option value=\"$a[$i]\">$a[$i]</option>\n";
}

?>

Link to comment
Share on other sites

Seems you have missed to check whether the form is posted or not. Here is the code that I edited. Let us know if it works or not.

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><?php

function dydx($x,$y){

  $equation = 3 * pow($x, 2);
  return $equation;
}

?>

<table>
<tr>
	<td>Enter number of partitions:</td>
	<td><input type="text" size="7" maxlength="7" name="partitions"
		value="" /></td>
</tr>
<tr>
	<td>Enter initial x value, x(0):</td>
	<td><input type="text" size="3" maxlength="3" name="initialx" value="" /></td>
</tr>
<tr>
	<td>Enter the ending x value:</td>
	<td><input type="text" size="3" maxlength="3" name="endingx" value="" /></td>
</tr>
<tr>
	<td>Enter initial y value, y(0):</td>
	<td><input type="text" size="3" maxlength="3" name="initialy" value="" /></td>
</tr>
</table>

</br>

<input type="submit" name="runge kutta name" value="Run Program" />
<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST' ) { // make sure that the form is posted

  $partitions = $_POST['partitions'];
  $initialx = $_POST['initialx'];
  $endingx = $_POST['endingx'];
  $initialy = $_POST['initialy'];

  if ($_POST['partitions'] != 0) {

    $h = ($endingx - $initialx)/$partitions;
  }


  $a[1] = $initialx;
  $b[1] = $initialy;

  for ($i = 1; $i <= $partitions; $i++) {

    $k1 = dydx($a[$i], $b[$i]);
    $k2 = dydx($a[$i] + (1/2)*$h, $b[$i] + (1/2)*$h*$k1);
    $k3 = dydx($a[$i] + (1/2)*$h, $b[$i] + (1/2)*$h*$k2);
    $k4 = dydx($a[$i] + $h, $b[$i] + $h*$k3);

    $a[$i + 1] = $a[$i] + $h;
    $b[$i + 1] = $b[$i] + (1/6)*$h*($k1 + 2*$k2 + 2*$k3 + $k4);
  }

  for ($i = 1; $i <= $partitions + 1; $i++) {

    $xyarray["$a[$i]"] = $b[$i];
  }

  $_SESSION['graph'] = $xyarray;

  ?>
  </br>
</br>

Choose a partition of X: <select name="output">

<?php

  for ($i = 1; $i <= $partitions + 1; $i++) {
  
    echo "<option value=\"$a[$i]\">$a[$i]</option>\n";
  }
} // if ($_SERVER['REQUEST_METHOD'] == 'POST' ) ends
?>
</form>

Link to comment
Share on other sites

Ok so here's what I have now...

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<table>
<tr>
	<td>Enter function y prime:</td>
	<td><input type="text" size="50" maxlength="" name="yprime" value="" /></td>
</tr>
<tr>
	<td>Enter number of partitions:</td>
	<td><input type="text" size="7" maxlength="7" name="partitions" value="" /></td>
</tr>
<tr>
	<td>Enter initial x value, x(0):</td>
	<td><input type="text" size="3" maxlength="3" name="initialx" value="" /></td>
</tr>
<tr>
	<td>Enter the ending x value:</td>
	<td><input type="text" size="3" maxlength="3" name="endingx" value="" /></td>
</tr>
<tr>
	<td>Enter initial y value, y(0):</td>
	<td><input type="text" size="3" maxlength="3" name="initialy" value="" /></td>
</tr>
</table>

</br>

<input type="submit" name="runge kutta name" value="Run Program" />

</form>

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST' ) { // make sure that the form is posted

$yprime = $_POST['yprime'];
$partitions = $_POST['partitions'];
$initialx = $_POST['initialx'];
$endingx = $_POST['endingx'];
$initialy = $_POST['initialy'];

function dydx($x,$y){

$equation = 2 * $x;
return $equation;
}

if ($_POST['partitions'] != 0) {

$h = ($endingx - $initialx)/$partitions;
}


$a[1] = $initialx;
$b[1] = $initialy;

for ($i = 1; $i <= $partitions; $i++) {

$k1 = dydx($a[$i], $b[$i]);
$k2 = dydx($a[$i] + (1/2)*$h, $b[$i] + (1/2)*$h*$k1);
$k3 = dydx($a[$i] + (1/2)*$h, $b[$i] + (1/2)*$h*$k2);
$k4 = dydx($a[$i] + $h, $b[$i] + $h*$k3);

$a[$i + 1] = $a[$i] + $h;
$b[$i + 1] = $b[$i] + (1/6)*$h*($k1 + 2*$k2 + 2*$k3 + $k4);
}

for ($i = 1; $i <= $partitions + 1; $i++) {

$xyarray["$a[$i]"] = $b[$i];
}

$_SESSION['graph'] = $xyarray;

} // if ($_SERVER['REQUEST_METHOD'] == 'POST' ) ends

?>

</br></br>

Choose a partition of X:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<select name="output">

<?php

for ($i = 1; $i <= $partitions + 1; $i++) {

echo "<option value=\"$b[$i]\">$a[$i]</option>\n";
}

?>

</select>

<input type="submit" name="runge kutta name" value="See Y" />

</form>



<?php

echo $_POST['output'];

 

I put in my second post command to display the y value for the corresponding x value, and it almost works. It successfully displays the value I expect it to but it wipes out all the data from the $a and $b arrays at the same time. I would like to preserve that data until new data is inputted for partitions, initial x, ending x, and initial y. This is where I'm having problems.

Link to comment
Share on other sites

Here you go :)

 

<table>
<tr>
	<td>Enter function y prime:</td>
	<td><input type="text" size="50" maxlength="" name="yprime" value="<?php echo (isset($_POST['yprime']) ? $_POST['yprime'] : '')?>" /></td>
</tr>
<tr>
	<td>Enter number of partitions:</td>
	<td><input type="text" size="7" maxlength="7" name="partitions" value="<?php echo (isset($_POST['partitions']) ? $_POST['partitions'] : '')?>" /></td>
</tr>
<tr>
	<td>Enter initial x value, x(0):</td>
	<td><input type="text" size="3" maxlength="3" name="initialx" value="<?php echo (isset($_POST['initialx']) ? $_POST['initialx'] : '')?>" /></td>
</tr>
<tr>
	<td>Enter the ending x value:</td>
	<td><input type="text" size="3" maxlength="3" name="endingx" value="<?php echo (isset($_POST['endingx']) ? $_POST['endingx'] : '')?>" /></td>
</tr>
<tr>
	<td>Enter initial y value, y(0):</td>
	<td><input type="text" size="3" maxlength="3" name="initialy" value="<?php echo (isset($_POST['initialy']) ? $_POST['initialy'] : '')?>" /></td>
</tr>
</table>

 

Link to comment
Share on other sites

WOW I'm not sure why I forgot to try this. You would think it would work, and it does keep the values in the forms, which I wanted to do anyways. But I just tested it and as soon as I hit that second submit button it still wipes out all my old data. Now I really don't know why, but I guess I don't fully understand how the SELF POST method works. Any other ideas? And thank you btw for helping me with this, it's actually part of a school project that's due Thursday so it really is a big help.

Link to comment
Share on other sites

If I understand the issue correctly, it sounds like you're having problems with losing data in the second form; the one that displays the drop-down menu. If that's the case, you need to create hidden variables for whatever you want to pass through the form. For example:

 

<input type="hidden" name="initialy" value="<?php echo $_POST['initialy']; ?>" />

 

 

Also, I would strongly recommend that you avoid using $_SERVER['PHP_SELF'] in your form action attribute. It's very easy to hack PHP_SELF when it's used in the action attribute, in links, or when it's displayed on screen.

http://www.mc2design.com/blog/php_self-safe-alternatives

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.