Page 1 of 1
There must be a better way to program this!
Posted: Sun Nov 22, 2009 9:16 am
by Gadgetmo
I
know there must be a better way to program this. Please help because it's not working at all!
Code: Select all
<html>
<body>
<form method="post" action="testget.php">
text: <input type="text" name="text"> <br />
width: <input type="text" name="wid" size="4" value="<?php if ($one=='1') { echo '400'; } else if ($two=='1') { echo '392'; } else if ($three=='1') { echo '350'; } else if ($four=='1') { echo '120'; } else if ($five=='1') { echo '234'; } else if ($six=='1') { echo '340'; } ?>"> <br />
height: <input type="text" name="hei" size="4" value="<?php if ($one==1) { echo '30'; } else if ($two==1) { echo '72'; } else if ($three==1) { echo '300'; } else if ($four==1) { echo '90'; } else if ($five==1) { echo '60'; } else if ($six==1) { echo '480'; } ?>"> <br />
<input type="submit">
</form> <br /><br /><br />
<a href="<?php $one = 1; $two = 0; $three = 0; $four = 0; $five = 0; $six = 0; ?>">400 by 30</a><br />
<a href="<?php $one = 0; $two = 1; $three = 0; $four = 0; $five = 0; $six = 0; ?>">392 by 72</a><br />
<a href="<?php $one = 0; $two = 0; $three = 1; $four = 0; $five = 0; $six = 0; ?>">350 by 300</a><br />
<a href="<?php $one = 0; $two = 0; $three = 0; $four = 1; $five = 0; $six = 0; ?>">120 by 90</a><br />
<a href="<?php $one = 0; $two = 0; $three = 0; $four = 0; $five = 1; $six = 0; ?>">234 by 60</a><br />
<a href="<?php $one = 0; $two = 0; $three = 0; $four = 0; $five = 0; $six = 1; ?>">340 by 480</a><br />
</body>
</html>
Thanks, Arthur.
Re: There must be a better way to program this!
Posted: Sun Nov 22, 2009 10:06 am
by onion2k
Can you explain what it's supposed to do?
Re: There must be a better way to program this!
Posted: Sun Nov 22, 2009 10:22 am
by cpetercarter
You are using a complicated and unusual way for selecting height and width values. Investigate
select boxes or
radio buttons.
Re: There must be a better way to program this!
Posted: Sun Nov 22, 2009 3:54 pm
by morris520
Code: Select all
<html>
<body>
<form method="post" action="testget.php">
text: <input type="text" name="text"> <br />
<?php
$widthVal = NULL;
if ($one=='1') { $widthVal = 400; }
else if ($two=='1') { $widthVal = 392; }
else if ($three=='1') { $widthVal = 350; }
else if ($four=='1') { $widthVal = 120; }
else if ($five=='1') { $widthVal = 234; }
else if ($six=='1') { $widthVal = 340; } ?>
width: <input type="text" name="wid" size="4" value="<?=$widthVal;?>"> <br />
<!-- DO IT YOURSELF -->
height: <input type="text" name="hei" size="4" value="$HeightVal;"> <br />
<input type="submit">
</form> <br /><br /><br />
<!-- WHAT ARE YOU DOING HERE? ARE THEY LINKS? -->
<a href="<?php $one = 1; $two = 0; $three = 0; $four = 0; $five = 0; $six = 0; ?>">400 by 30</a><br />
<a href="<?php $one = 0; $two = 1; $three = 0; $four = 0; $five = 0; $six = 0; ?>">392 by 72</a><br />
<a href="<?php $one = 0; $two = 0; $three = 1; $four = 0; $five = 0; $six = 0; ?>">350 by 300</a><br />
<a href="<?php $one = 0; $two = 0; $three = 0; $four = 1; $five = 0; $six = 0; ?>">120 by 90</a><br />
<a href="<?php $one = 0; $two = 0; $three = 0; $four = 0; $five = 1; $six = 0; ?>">234 by 60</a><br />
<a href="<?php $one = 0; $two = 0; $three = 0; $four = 0; $five = 0; $six = 1; ?>">340 by 480</a><br />
</body>
</html>
Re: There must be a better way to program this!
Posted: Mon Nov 23, 2009 4:09 am
by Gadgetmo
I've found a
much better way to program it, which involves Javascript (and PHP). Here it is:
Code: Select all
<?php $one = $_GET['one']; $two = $_GET['two']; $three = $_GET['three']; $four = $_GET['four']; $five = $_GET['five']; $six = $_GET['six']; $text = $_GET['text']; ?>
<html>
<body>
<script type="text/javascript">
<!--
var textField = document.getElementById('text');
//-->
</script>
<form method="post" action="testget.php">
text: <input type="text" name="text" id="text" value="<?php echo $text; ?>"> <br />
width: <input type="text" name="wid" size="4" value="<?php if ($one=='1') { echo '400'; } else if ($two=='1') { echo '392'; } else if ($three=='1') { echo '350'; } else if ($four=='1') { echo '120'; } else if ($five=='1') { echo '234'; } else if ($six=='1') { echo '340'; } ?>"> <br />
height: <input type="text" name="hei" size="4" value="<?php if ($one==1) { echo '30'; } else if ($two==1) { echo '72'; } else if ($three==1) { echo '300'; } else if ($four==1) { echo '90'; } else if ($five==1) { echo '60'; } else if ($six==1) { echo '480'; } ?>"> <br />
<input type="submit">
</form> <br /><br /><br />
<script type="text/javascript">document.write('<a href="testform.php?one=1&text='+textField+'">400 by 30</a><br />');</script>
<script type="text/javascript">document.write('<a href="testform.php?two=1&text='+textField+'">468 by 60</a><br />');</script>
<script type="text/javascript">document.write('<a href="testform.php?three=1&text='+textField+'">392 by 72</a><br />');</script>
<script type="text/javascript">document.write('<a href="testform.php?four=1&text='+textField+'">120 by 90</a><br />');</script>
<script type="text/javascript">document.write('<a href="testform.php?five=1&text='+textField+'">640 by 480</a><br />');</script>
<script type="text/javascript">document.write('<a href="testform.php?six=1&text='+textField+'">234 by 60</a><br />');</script>
</body>
</html>
The only problem is that
always returns null. How do I fix this??
Thanks for all the other suggestions,
Arthur
Re: There must be a better way to program this!
Posted: Mon Nov 23, 2009 5:39 am
by Gadgetmo
I tried putting
on the end, but then it returned undefined. This is really weird

Posted: Mon Nov 23, 2009 12:40 pm
by Jonah Bron
That javascript is executed in the header, when the element 'text' doesn't exist. It hasn't loaded yet.
Re: There must be a better way to program this!
Posted: Mon Nov 23, 2009 12:51 pm
by onion2k
Have you actually explained what you're trying to do yet?
Re: There must be a better way to program this!
Posted: Tue Nov 24, 2009 5:31 am
by morris520
I think you just tried to pass variables to the page itself right?
if so just use html <a href="<?=$_SERVER['PHP_SELF'];?>?one=1&text=TextField">400 by 30</a>
if your var text value is from a Form Element, then do it through a JS function, document.getElementById('TextField').value
The way you do it is still complicated.
Suggest you can grab a book to read.