Jump to content

I don't know if this is even possible


Pandolfo

Recommended Posts

Hey everyone,

 

I am working on something that has turned into a monster. I have some html containing a select form which i have assigned to a variable. I understand how to echo the variable..no problem there. The problem is that i need to validate the form, grab the selected value, and assign it to a new variable. The code (in part anyway) is listed below.

 

$timesplitter= "<table align=center width=100% border=1 cellpadding=0 cellspacing=0><tr><td align=left width=100% class=even><font color=red>Percentage of time to spend for work and work related activities : </font> <select name=timesplit size=1><option value=0>Do not change/Keep Last years value<option value=10>10%<option value=20>20%<option value=30>30%<option value=40>40%<option value=50>50%<option value=60>60%<option value=70>70%<option value=80>80%</select></td> </tr> </table></td> </tr> </table>";
echo $timesplitter;

 

There's the form. Normally i would use POST to grab the value and set it equal to a new variable. I don't know how to do that in this case. Any help or insight you might be able to offer would really be appreciated.

 

Thanks!

Link to comment
Share on other sites

Okay, first LINE BREAKS ARE YOUR FRIEND!

 

$timesplitter= "<table align=center width=100% border=1 cellpadding=0 cellspacing=0>
  <tr>
    <td align=left width=100% class=even>
  <font color=red>
  Percentage of time to spend for work and work related activities : 
  </font>
  <select name=timesplit size=1>
    <option value=0>Do not change/Keep Last years value<option value=10>10%
	<option value=20>20%
	<option value=30>30%
	<option value=40>40%
	<option value=50>50%
	<option value=60>60%
	<option value=70>70%
	<option value=80>80%
      </select>
</td>
  </tr> 
</table>

</td> </tr> </table>";
echo $timesplitter;

 

Okay, second.  To output all that html, you should simply drop out of PHP.

 

<?php

// lots of code here

?>
<table>
  <tr>
    <td>
    </td>
  </tr>
</table>
<?php

// more code here

?>

 

There is no need to assign all that to a variable.

 

Third.  A select tag has to be contained within form tags.  You also need to close your option tags.

Link to comment
Share on other sites

hmm...okay. You mean something like this?

 

$divorcestatement="Your spouse leaves you a note stating that you've neglected the marriage and children too much and divorce is
the only option. Divorce results in a fifty percent (50%) reduction of all assets. When the dust settles your net assests total
'$newassets'. This change will be reflected in next year's financial statement. Soon after the divorce is settled you meet someone new.
Your are currently married/dating again.<br><br>";
	echo $divorcestatement;		
	?>
	<table align=center width=100% border=1 cellpadding=0 cellspacing=0>
  <tr>
    <td align=left width=100% class=even>
  <font color=red>
  Percentage of time to spend for work and work related activities : 
  </font>
  <form name="timesplitter">
  <select name=timesplit size=1>
    <option value=0>Do not change/Keep Last years value<option value=10>10%
	<option value=.20>20%</option>
	<option value=.30>30%</option>
	<option value=.40>40%</option>
	<option value=.50>50%</option>
	<option value=.60>60%</option>
	<option value=.70>70%</option>
	<option value=.80>80%</option>
      </select>
</form>
</td>
  </tr> 
</table>

</td> </tr> </table>
<?
$worktime = $_POST['timesplitter'];	

Link to comment
Share on other sites

That whole $divorcestatement variable can be done away with as well.

 

Your spouse leaves you a note stating that you've neglected the marriage and children too much 
and divorce is the only option. Divorce results in a fifty percent (50%) reduction of all assets. 
When the dust settles your net assests total <?=$newassets?>. This change will be reflected in 
next year's financial statement. Soon after the divorce is settled you meet someone new. Your 
are currently married/dating again.<br><br>

<table align=center width=100% border=1 cellpadding=0 cellspacing=0>
  <tr>
    <td align=left width=100% class=even>
      <font color=red>
        Percentage of time to spend for work and work related activities : 
      </font>
      <form name="timesplitter">
      <select name=timesplit size=1>
        <option value=0>Do not change/Keep Last years value<option value=10>10%
        <option value=.20>20%</option>
        <option value=.30>30%</option>
        <option value=.40>40%</option>
        <option value=.50>50%</option>
        <option value=.60>60%</option>
        <option value=.70>70%</option>
        <option value=.80>80%</option>
      </select>
      </form>
    </td>
  </tr> 
</table>

</td> </tr> </table>
<?
$worktime = $_POST['timesplitter'];

 

Using the POST array at the end of the file isn't going to do what you think it will.  If you want to use the data that the user enters into the timesplitter form, you're going to have to do something like this.

 

<form method="post" action="another_page.php">
  <select>
    <!-- lots of options go here -->
  </select>
  <input type="submit" name="submit" value="Submit Data">
</form>

 

By clicking the submit button, the data in the form gets sent to "another_page.php" and is processed by that script.  There isn't a way to get at the data in the form on the current page using PHP.  For something like that you want JavaScript.  Remember, all PHP does is spit out a document, in this case an html file.  Once it's done generating that document, it won't do anything until you tell it to generate another document (e.g. click on a link that takes you to another PHP script).

 

Link to comment
Share on other sites

There isn't a way to get at the data in the form on the current page using PHP.

 

If you don't specify a form action, the default is to submit to the same page, same as if you specify $_SERVER['PHP_SELF'] as the action.

 

<?php
if (isset($_GET['do']))
{
    echo "<p>{$_GET['selection']}</p><hr />";
}  
?>
<form>
<select name="selection">
  <option value="option 1">option 1</option>
  <option value="option 2">option 2</option>
  <option value="option 3">option 3</option>
</select>
<br />
<input type="submit" name="do" value="Submit">
</form>

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.