Jump to content
xisto Community
Sign in to follow this  
sonesay

Php An Js Window.open Pages Trouble. I need a way to set hidden input values to the new window.

Recommended Posts

I have 2 main pages Page A(events_locked.php) and Page B(add_attendance.php). Both are php files.

Page A takes a post var from another page(not Page B ) and then used to query for displaying records in a mysql datase. This variable has set as a session variable because there is 1 <input type='submit' on Page A that will invoke a javascript function to open a new window.

<script LANGUAGE="JavaScript">window.name="main_index";function openFormWindow(){ 	OpenWindow=window.open("add_attendance.php", "newwin", "height=250, width=400,toolbar=no,scrollbars=no,menubar=no,location=no,resizable=no");var x = getElementByName("form1");x.target="newwin";x.submit();}

As you can see the first parameter on openFormWindow() is Page B php file Im wanting to display. The php file has some database queries in it and uses that preivous set session var to identify what records i want. When I use the window.open() from javascript it creates a nice new window for me but I cannot assign any <input type='hidden' values in it. I am going to be using 2 or more buttons on Page A thats going to go to page B and i need to be able to able to identify what is being submitted to display correct content for the user. The post count on page B is 0 so there is no way I am able to grab them.

heres the code for page B it might help in understanding what I am trying to do abit better.

$s['ea_added_group'] = false;





// get list of members
$m_query = "SELECT userid, username FROM users WHERE userid NOT IN (SELECT ea_user_id FROM events_attend WHERE ea_event_id =" . $event_id . ")";
$m_result = mysql_query($m_query, $link) or die("Cannot return users list");



// get event details
$e_query = "SELECT * FROM events WHERE e_id = " . $event_id;
$e_result = mysql_query($e_query, $link);
while ($e = mysql_fetch_array($e_result)) {
$e_start = $e['e_start'];

}

if ($p['mode'] == "") {
$e_start = date("H linenums:0'><?phpsession_start();// include database fileinclude("includes/database_itrap.php");$event_id = $s['event_id'];$s['ea_added_group'] = false;// get list of members $m_query = "SELECT userid, username FROM users WHERE userid NOT IN (SELECT ea_user_id FROM events_attend WHERE ea_event_id =" . $event_id . ")";$m_result = mysql_query($m_query, $link) or die("Cannot return users list");// get event details$e_query = "SELECT * FROM events WHERE e_id = " . $event_id;$e_result = mysql_query($e_query, $link);while ($e = mysql_fetch_array($e_result)) { $e_start = $e['e_start'];}if ($p['mode'] == "") { $e_start = date("H:i:s");}?><html><head><title>Add Attendance</title><script type='text/javascript'>function addUser(toGroup,fromGroup) { var listLength = document.getElementById(toGroup).length; var selItem = document.getElementById(fromGroup).selectedIndex; var selText = document.getElementById(fromGroup).options[selItem].text; var selValue = document.getElementById(fromGroup).options[selItem].value; var i; var newItem = true; for (i = 0; i < listLength; i++) { if (document.getElementById(toGroup).options.text == selText) { newItem = false; break; } } if (newItem) { document.getElementById(toGroup).options[listLength] = new Option(selText, selValue); document.getElementById(fromGroup).options[selItem] = null; }}function saveGroup() { var strValues = ""; var boxLength = document.getElementById('group2').length; var elcount = 0; if (boxLength != 0) { for (i = 0; i < boxLength; i++) { if (elcount == 0) { strValues = document.getElementById('group2').options.value; } else { strValues = strValues + "," + document.getElementById('group2').options.value; } elcount++; } } if (strValues.length == 0) { return false; } else { document.forms['groupsform'].group_users.value = strValues; document.forms['groupsform'].submit(); return true; }}function ckForm() { var tmp = saveGroup(); return tmp;}function test () { alert(document.forms.groupsform.mode.value);}</script></head><body onLoad="test();"><form name="groupsform" method="post" action="events_locked.php" target="main_index" onSubmit="setTimeout('window.close()', 0000); return ckForm();"> <div align="center">Event ID: <?php echo $event_id . " "; ?>Start Time: <input type="text" name="ea_start" value="<?php echo $e_start; ?>" size="10" /><br /><select multiple='multiple' size='10' id='group1' name='group1' style='width:150' onChange="addUser('group2','group1');" ><?phpwhile($u = mysql_fetch_array($m_result)) { echo "<option value='" . $u['userid'] . "'>" . ucfirst($u['username']) . "</option>";}?></select><select multiple='multiple' size='10' id='group2' name='group2' style='width:150' onChange="addUser('group1','group2');" ></select><input type='hidden' name='group_users' /><input type="hidden" name="event_id" value="<?php echo $event_id; ?>" /><input type="hidden" name="mode" value="" /><input type="submit" name="save_group" value="submit form" /> <input type="submit" value="Close" onClick="window.close();" /><br /><br />Post count: <?php echo count($_POST); ?>Mode: <?php echo $_POST['mode']; ?></div></form></body></html>
I seem to be posting alot of issues with how I want to do things in web. I have read/seen abit of ajax but still not proficent in PHP, XHTML, or JS enough to want to dive into it at this stage. Im still only starting to understand the DOM. I look on the web for any examples to help but there is nothing close to what I want to do so far. I sometimes wonder am I going about this the wrong way. sorry for the rant please someone help me out.
Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

Just to make sure I got you right... You have post data being submited to page A, and page A opens page B with javascript, and you want page B to have that post data?

If this is it, there is quite a simple solution...

Set the post data as get data when opening the next page with javascript (see below) and replace all $_POST's with $_GETS in page B.

function openFormWindow(){OpenWindow=window.open("add_attendance.php?<?php$firstrun=true;foreach($_POST as $i => $data){if($firstrun)$firstrun=false;else echo'&';echo $i."=".$data;}?>", "newwin", "height=250, width=400,toolbar=no,scrollbars=no,menubar=no,location=no,resizable=no");var x = getElementByName("form1");x.target="newwin";x.submit();}

If this is not what you meant please re-explain what exactly you are trying to do...
Edited by alex7h3pr0gr4m3r (see edit history)

Share this post


Link to post
Share on other sites

Yes thats right I'll give that a go now thanks ^^Update:I've tried adding those line of code it seems to work but somehow the mode variable gets stuck and cannot be changed. It gets set to 'add_on_time' on the URL for 'mode' even when I click the second button to invoke changemode('add_late'). I do get the alert in the function that tells me alert(document.forms.form1.mode.value) is set to 'add_late' but once the page gets created its still set mode='add_on_time'. I've tried to change $_POST to $_GET and $_REQUEST but still no luckScript from Page A

<script LANGUAGE="JavaScript">window.name="main_index";function openFormWindow(){ 	OpenWindow=window.open("add_attendance.php?<?php$firstrun=true;foreach($_POST as $i => $data){	if($firstrun)$firstrun=false;	else echo'&';		echo $i."=".$data;}?>", "newwin", "height=500, width=400,toolbar=no,scrollbars=no,menubar=no,location=no,resizable=no");var x = getElementByName("form1");x.target="newwin";x.submit();}function modechange(action) {document.forms.form1.mode.value = action;alert(document.forms.form1.mode.value);openFormWindow();}</SCRIPT>php form echo "<form method='get' name='form1'>";		echo "<input type='hidden' name='event_id' value='".$e['e_id']."' />";		echo "<input type='hidden' name='mode' value='Default' />";		echo "<input type='submit' value='Add On Time' onclick=\"modechange('add_on_time');\" />";				echo "<input type='button' value='Add Late' onclick=\"modechange('add_late');\" />";		echo "<input type='button' value='Add End Time' onclick='openFormWindow();' />";		echo "<input type='button' value='Add Leave' onclick='openFormWindow();' />";		echo "</form>";

Script from Page B (testing output)

Post count: <?php echo count($_POST) . " "; ?>Get count: <?php echo count($_GET) . " <br /> "; ?>Request count: <?php echo count($_REQUEST) . " <br /> "; ?>Request mode: <?php  echo  $_REQUEST['mode']. " <br /> ";  ?>Post mode: <?php  echo  $_POST['mode']. " <br /> ";  ?>Get mode: <?php  echo  $_GET['mode']. " <br /> ";  ?>

for some reason i still get the URL add_attendance.php?event_id=4&mode=add_on_time from using the first or second button. The changemode() does alert a new value is set to 'mode' but it still isnt saved on the newly open window. I have tried using sessions but since Javascript was used to make the page no session variables are available on the new window.UPDATE:Sessions seems to work now on window.open pages. It worked first time round then somehow i butched up my code and it stoped working. I messed it up so much I had to revert back to yesterdays backup. Well ima give it another go.

Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

Just an update. I guess the problem seems to be my function modechange() somehow not working with the openFormWindow(). I dont know if its because the very first page (Not Page A thats events_locked.php) only sends 1 post variable (event_id) so the function loop in page A for post dosent include 'mode' somehow.

 

Well my current porblem now is I need to be able to set the <input type='hidden' name='mode' value so I can control what gets displayed on attendance.php (Page :P. My modechange() does alert me that its changed but when page B gets created its not set and somehow always defaults to mode='add_on_time' value no matter what button I push on page A (add on time button or add late button).

 

Heres an image it might give a clear idea of what im trying to do. If i can control the 'mode' to = 'add_late' I can set the default time to current time.

 

Posted Image

 

Edit: I spend the day reading up on abit of ajax and found an easy example. I backed up the files at this current stage since I still cant get it to work and am going to try to do it without using a pop up window. I think keeping it simple and working on the same page will see the site get done quicker. I can always go back to it and try get a working version of a pop up window at a later date.

Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.