There must be a better way to program this!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gadgetmo
Forum Newbie
Posts: 14
Joined: Sun Nov 01, 2009 7:17 am

There must be a better way to program this!

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: There must be a better way to program this!

Post by onion2k »

Can you explain what it's supposed to do?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: There must be a better way to program this!

Post by cpetercarter »

You are using a complicated and unusual way for selecting height and width values. Investigate select boxes or radio buttons.
User avatar
morris520
Forum Commoner
Posts: 60
Joined: Thu Sep 18, 2008 8:56 pm
Location: Manchester UK

Re: There must be a better way to program this!

Post 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>
 
Gadgetmo
Forum Newbie
Posts: 14
Joined: Sun Nov 01, 2009 7:17 am

Re: There must be a better way to program this!

Post 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

Code: Select all

textField
always returns null. How do I fix this??

Thanks for all the other suggestions,
Arthur
Gadgetmo
Forum Newbie
Posts: 14
Joined: Sun Nov 01, 2009 7:17 am

Re: There must be a better way to program this!

Post by Gadgetmo »

I tried putting

Code: Select all

.innerHTML
on the end, but then it returned undefined. This is really weird :crazy:
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

That javascript is executed in the header, when the element 'text' doesn't exist. It hasn't loaded yet.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: There must be a better way to program this!

Post by onion2k »

Have you actually explained what you're trying to do yet?
User avatar
morris520
Forum Commoner
Posts: 60
Joined: Thu Sep 18, 2008 8:56 pm
Location: Manchester UK

Re: There must be a better way to program this!

Post 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.
Post Reply