The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Setting a Variable within a Form (Html/JScript/Php)

Sharp101Sharp101 TorontoRegistered User regular
edited March 2007 in Help / Advice Forum
Hey Someone

I'm writing some code to sort some lists with drag and drop, then enter your name into a text field, and it sends info to a php script which displays how many elements in each list. (link to requirements here)

Anyway, it has to all be done a certain way, and I've done everything except I have to set a hidden form element to a certain string to send off to the php script, and Its not working. It is a requirement to do this though post, and using $_Request (am I doing that part right? I'm a little confused to what $_Request is)

The line in question is commented around, without that line it will pop up with the correct string, but with it in it wont do anything.

If anyone wants me to spoiler this for the sake of the form, I will.

The JS/Html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd"> 

<title>CPS630 Lab 2</title>

<style type="text/css">
.hidden {
visibility:hidden;
margin-top:-20px;
}
</style>

&lt;script type="text/javascript" src="/dojoSrc/dojo.js"></script>
&lt;script type="text/javascript">
	dojo.require("dojo.dnd.*");
	dojo.require("dojo.event.*");
	dojo.require("dojo.widget.*");
	dojo.require("dojo.widget.Button");
		
	function byId(id){
		return document.getElementById(id);
	}
	
	function updateVars(){
		var dl = byId("clothList");
	var lis2 = dl.getElementsByTagName("li");
	var list2Count = lis2.length - 1;
	var dl = byId("foodList");
	var lis3 = dl.getElementsByTagName("li");
	var list3Count = lis3.length - 1;
	
	var countToSend = "Food:"+list2Count+";Clothes:"+list3Count;
	
	/////////////////////////////
	// document.myForm.lists.value = countToSend;
	////////////////
	alert(countToSend);
	}
	
	function buttonPressed()
	{

		updateVars();
		
		dojo.io.bind({
					   url: 'getCount.php',
					   handler: buttonCallback,
					   formNode: dojo.byId('myForm')
					});
	}
	
	function buttonCallback(type, data, evt)
	  {
		if (type == 'error')
		  alert('Error when retrieving data from the server!');
		else{
		  updateStatus(data);

		  }
	  }
	  
   function updateStatus(msg)
	{		
	if(elem = document.getElementById("statusmessage"))
		elem.innerHTML = msg;
	}

	function init(){
		// list one
		var dl = byId("mainList");
		new dojo.dnd.HtmlDropTarget(dl, ["li11", "li12", "li2", "li3"]);
		var lis = dl.getElementsByTagName("li");
		for(var x=0; x<lis.length; x++){
		if(x<4)
			new dojo.dnd.HtmlDragSource(lis[x], "li11");
		else
			new dojo.dnd.HtmlDragSource(lis[x], "li12");
			
		}

		// list two
		var dl = byId("clothList");
		new dojo.dnd.HtmlDropTarget(dl, ["li11"]);
		var lis = dl.getElementsByTagName("li");
		for(var x=0; x<lis.length; x++){
			new dojo.dnd.HtmlDragSource(lis[x], "li2");
		}

		// list three
		var dl = byId("foodList");
		new dojo.dnd.HtmlDropTarget(dl, ["li12"]);
		var lis = dl.getElementsByTagName("li");
		for(var x=0; x<lis.length; x++){
			new dojo.dnd.HtmlDragSource(lis[x], "li3");
		}
		var sumbitButton = dojo.widget.byId('sumbitButton');
		dojo.event.connect(sumbitButton, 'onClick', 'buttonPressed')
	}

	dojo.event.connect(dojo, "loaded", "init");
</script>
<p>Drag Items from The Main List into the coresponding lists below. Clothing Items will only be accepted by the Clothing list, and Food Items will only be accepted by the Food List.</p>

<p><b>Main List</b></p>
<ul id="mainList">
	<li>Pants</li>
	<li>Shirts</li>
	<li>Shoes</li>
	<li>Socks</li>
	<li>Peanuts</li>
	<li>Hamburgers</li>
	<li>Candy</li>
	<li>Bread</li>
</ul>

<p>List One: <b>Clothing</b></p>
<ul id="clothList">
<li class="hidden">test</li>
</ul>

<p>List Two: <b>Food</b></p>
<ul id="foodList">
<li class="hidden">test</li>
</ul>
<form id="myForm" method="post">
		<input type="text" id="name" name="name" />
		<input type="hidden" id="lists" name="lists" />
</form>
		<button dojoType="button" id="sumbitButton">DOJO BUTTONS ARE COOL</button>

<br /><br />
		<div id="statusmessage"></div>

I've also tried
<input type="hidden" id="lists" name="lists" value=countToSend />

but that doesn't work either.




the php:
<?php
/****************
// getCount.php
****************/
ereg('((([a-z]|[A-Z]|[0-9]|_)*):([0-9]*);)((([a-z]|[A-Z]|[0-9]|_)*):
([0-9]*);)', $_REQUEST['lists'], $matches);
header('Content-type: text/plain');
printf("Thanks {$_REQUEST['name']}.<br />Your Lists: $matches[2]
($matches[4]) and $matches[6] ($matches[8])<br />");
?>
This is due tonight, and I'm so damn close.

Sharp101 on

Posts

  • blincolnblincoln Registered User regular
    edited March 2007
    Have you tried in your PHP enumerating everything that's in the $REQUEST variable to see if your web page is even passing it anything?

    blincoln on
    Legacy of Kain: The Lost Worlds
    http://www.thelostworlds.net/
  • ÆthelredÆthelred Registered User regular
    edited March 2007
    Why are you using $_REQUEST? Use $_POST.

    Æthelred on
    pokes: 1505 8032 8399
  • Sharp101Sharp101 TorontoRegistered User regular
    edited March 2007
    Æthelred wrote: »
    Why are you using $_REQUEST? Use $_POST.
    It is a requirement to do this though post, and using $_Request

    The php script is given to us and we cannot change it (we just hand in out html file)



    I'll try printing out what the php is receiving now.

    Sharp101 on
  • Sharp101Sharp101 TorontoRegistered User regular
    edited March 2007
    ok so using this php script (trying to print different ways)
    <?php
    
    $names = $_POST['name'];
    
    header('Content-type: text/plain');
    echo test;
    echo $names;
    echo test2;
    echo $_REQUEST['lists'];
    
    ?>
    
    prints nothing.

    <?php
    
    $names = $_POST['name'];
    
    header('Content-type: text/plain');
    echo </br>test;
    echo $names;
    echo test2;
    echo $_REQUEST['lists'];
    
    ?>
    

    prints out

    test; echo $names; echo test2; echo $_REQUEST; ?>

    so, I dont think it's sending anything, or/and the php and/or the html isn't working properly

    any ideas?

    Sharp101 on
  • blincolnblincoln Registered User regular
    edited March 2007
    There should be a way to enumerate *everything* in $_REQUEST. I don't usually work in PHP, so I can't remember offhand. I'll try to dig it up later.

    blincoln on
    Legacy of Kain: The Lost Worlds
    http://www.thelostworlds.net/
  • Sharp101Sharp101 TorontoRegistered User regular
    edited March 2007
    yeah, but there is even a farther problem....

    a simple script like this
    <?php
    $testVar = "wee";
    
    print "<br />$testVar;";
    ?>
    

    will just print out

    $testVar;" ?>

    (note the ?> at the end....)

    so somethings very wrong.

    Sharp101 on
  • ÆthelredÆthelred Registered User regular
    edited March 2007
    You don't have a semicolon at the end of your second line.

    edit: okay now you do. Try using echo instead of print mebbe. Also, get rid of the first semicolon in the fourth line.

    Æthelred on
    pokes: 1505 8032 8399
  • JaninJanin Registered User regular
    edited March 2007
    That code gave me a syntax error. I corrected it to this code, and it worked fine on my system. Try copy-pasting this code onto the server - if it doesn't work right, talk to your instructor.
    <?php
    $testVar = "wee";
    
    print "<br />$testVar";
    ?>
    

    Janin on
    [SIGPIC][/SIGPIC]
  • Sharp101Sharp101 TorontoRegistered User regular
    edited March 2007
    ok, so that doesn't work. (I see my fucked up ;'s now)

    Something must be wrong with my server set up.

    but, the line of code
    <input type="hidden" id="lists" name="lists" value=countToSend />

    should work (combined with my original code up top) right?


    I'm thinking I'm just going to hand this in and talk to my professor tomorrow.

    Sharp101 on
Sign In or Register to comment.