Page 1 of 1

Problems Refershing Browser

Posted: Mon Jan 02, 2006 3:31 am
by ToeShot
Hello

First I want to say that I am fairly new to PHP and Web Developement. I have created web pages before, but not entire sites. So I am in a position now were I need to develope a web site. I chose to go with PHP because of it's flexability and similarity to other languages. Also, as a side note, I am not familar with all of the HTML tags. So yes I may have jumped in some deep water. But I know how to swim and there are plenty of floatation devices on the internet, also this is an opportunity to learn another skill to put under my belt and I am having fun with it.

Ok so here is my problem and maybe my design is wrong so if someone knows a different way please let me know.

I have a page called admin.php, so far all this page does is grab information from a database (MySQL) and displays it in a table. At the bottom of the table there is a text box, two radio buttons, and a submit button. In the textbox the user enters the number of the item to be edited or deleted, depending on which radio button is selected. Edit is selected by default.

When the submit button is clicked the admin page calls up the edit.php page. This page has a switch/case statement with a few functions. The default of the switch/case statement is a call to the init function which initializes the variables with the selection the the user made from the database.

Now what I want to do is, after the init function is called is to refresh the browser. My flag variable is set and I want to fall back through my switch/case statement to take the proper action for what the user selected.

I have tried the header statement like this header("Location: $_SERVER['PHP_SELF']"); this did not work, so I tried this header("Refresh: $_SERVER['PHP_SELF']"); this also did not work. I have also tried the <meta> tag and I was not successful with that either. So I am stuck on how to get this to work. Any help would be appreciated.

Posted: Mon Jan 02, 2006 6:30 am
by amlan_08
I don't get you properly. Please clear few things for me.

1. User give the item no ( id ) to that text box to Edit/Delete.

2. Suppose you want to edit any item, then you go to the edit.php page

3. In edit.php page init function called, and the variable is set upon user's selection. On the basis of that variable the proper function like edit, delete will be called.

Is there any prob of my understanding?

Now what is the purpose of page refreshing. If you don't want to write the Edit code in that function then just create a new page like edit_itam.php and write that edit code there. In the function you just redirect the page to this new page like

header("location: edit_itam.php?val=1")

Is there any prob in this? If yes then please mention.

Also your design is wrong ( according to me ). I think the follwoing design is more userfrindly


Code: Select all

<tr>
      <td id="paginate" colSpan="6" height="15">User 1 to 1 of 1
        &nbsp;&nbsp;&nbsp;</td>
    </tr>
    <tr>
      <td height="15"></td>
    </tr>
    <tr>
      <td><input id="submitstyle" type="submit" value="Delete" name="delete">
      </td>
    </tr>
    <tr>
      <td width="27%">&nbsp;</td>      
      <td id="tdheading2">User Name</td>
      <td id="tdheading2">Email</td>
      <td id="tdheading2">Status</td>
      <td id="tdheading2">Edit</td>
      <!-- <td id="tdheading2">Edit</td> -->
    </tr>
    <tr>
      <td id="link" align="left" colSpan="6">&nbsp;<a id="mycursor" onclick="return check_all();">Check
        All</a>&nbsp;/&nbsp;<a id="mycursor" onclick="return uncheck_all();">Uncheck
        All</a></td>
    </tr>
    <tr bgColor="#aabbbc">
      <td id="records"><input type="checkbox" value="1" name="id[]"></td>      
      <td id="records">amlan</td>
      <td id="records">abc@yahoo.co.in</td>
      <td id="records">Active</td>
      <td id="records"><a href="#">Edit</a></td>      
    </tr>
Please mention if you find any prob in this

Browser Refresh Problem

Posted: Mon Jan 02, 2006 6:59 am
by ToeShot
hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have posted the code below for the edit.php page. maybe that will clear up what I am trying to do. not all the case statements are finished yet. I can't have my variables initialize everytime the page loads that is why I have it in a seperate function. I need to have $edit maintain its' value so I know what action to take in my switch/case statement.

Code: Select all

<?PHP
 require "../db_common.php";
 global $db_table_menu, $item_id, $edit, $item_number, $item_name, $item_desc, $row_id, $action_to_take, $item_price;

 db_connect();

 switch ($action_to_take) {
 
   case "Edit":
	show_data();
	create_form();
	$action_to_take = "update";
	echo "<br><INPUT type=\"submit\" value=\"Make Change\"><br>";
  	echo "</form>";
	break;

   case "Delete":
	show_data();
	$action_to_take = "remove";
	echo "<br><INPUT type=\"submit\" value=\"Delete Item\"><br>";
  	echo "</form>";
	break;

   case "update":
	$update_query = "UPDATE $db_table_menu SET ITEM_NAME=$item_name, ITEM_DSCRIPTION=$item_desc, ITEM_PRICE=$item_price WHERE ITEM_NO='$item_id'";
	$action_to_take = "remove";
	echo "<br><INPUT type=\"submit\" value=\"Delete Item\"><br>";
  	echo "</form>";
	break;

   case "remove":
	show_data();
	$action_to_take = "remove";
	echo "<br><INPUT type=\"submit\" value=\"Delete Item\"><br>";
  	echo "</form>";
	break;

   default:
	init();
            {{ I NEED TO REFRESH BROWSER HERE }}
	break;
 }


 function create_form() {
 
  global  $item_number, $item_name, $item_desc, $item_price;
 
  ?>
   <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
   <table>
    <tr>
     <td><LABEL for="item_no">Number:</LABEL></td>
     <td><INPUT size="2" type="text" name="item_no" value="<?php echo $item_number ?>"></td>
    </tr>
    <tr>
     <td><LABEL for="item_name">Name:</LABEL></td>
     <td><INPUT type="text" name="item_name" value="<?php echo $item_name ?>"></td>
    </tr>
    <tr>
     <td><LABEL for="item_desc">Description:</LABEL></td>
     <td><INPUT size="100" type="textbox" name="item_desc" value="<?php echo $item_desc ?>"></td>
    </tr>
    <tr>
     <td><LABEL for="item_price">Price:</LABEL></td>
     <td><INPUT size="6" type="text" name="item_price" value="<?php echo $item_price ?>"></td>
    </tr>
   </table>

 
  <?PHP
 }
 
 function show_data() {
 
  global  $item_number, $item_name, $item_desc, $item_price;
  
  ?>

  <br>
  <table align="center" border="1" cellpadding="3" width="90%">
    <tr>
      <td>Current Values</td>
      <td><?PHP echo $item_number ?></td>
      <td><?PHP echo $item_name ?></td>
      <td><?PHP echo $item_desc ?></td>
      <td><?PHP echo $item_price ?></td>
    </tr>
  </table>
  <br><br><br><br>

  <?PHP
 }

 function init() {

  global $db_table_menu, $item_id, $edit, $item_number, $item_name, $item_desc, $row_id, $action_to_take, $item_price;

  $row_id = $_POST['item_id'];
  $action_to_take = $_POST['edit'];
  $query = "SELECT * FROM $db_table_menu WHERE ITEM_NO='$row_id'";
  $display_row = mysql_query($query);
  $row_data = mysql_fetch_array($display_row);
  $item_number = $row_data['ITEM_NO'];
  $item_name = $row_data['ITEM_NAME'];
  $item_desc = $row_data['ITEM_DESCRIPTION'];
  $item_price = $row_data['ITEM_PRICE'];

 }
  ?>
hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Correction on variable

Posted: Mon Jan 02, 2006 7:01 am
by ToeShot
it is not the $edit variable but the $action_to_take variable that I need to maintain its' value. sorry for the confusion.

Re: Browser Refresh Problem

Posted: Mon Jan 02, 2006 9:25 am
by trukfixer
ToeShot wrote:hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have posted the code below for the edit.php page. maybe that will clear up what I am trying to do. not all the case statements are finished yet. I can't have my variables initialize everytime the page loads that is why I have it in a seperate function. I need to have $edit maintain its' value so I know what action to take in my switch/case statement.

Code: Select all

<?PHP
 require "../db_common.php";
 global $db_table_menu, $item_id, $edit, $item_number, $item_name, $item_desc, $row_id, $action_to_take, $item_price;

 db_connect();

 switch ($action_to_take) {
 
   case "Edit":
	show_data();
	create_form();
	$action_to_take = "update";
	echo "<br><INPUT type="submit" value="Make Change"><br>";
  	echo "</form>";
	break;

   case "Delete":
	show_data();
	$action_to_take = "remove";
	echo "<br><INPUT type="submit" value="Delete Item"><br>";
  	echo "</form>";
	break;

   case "update":
	$update_query = "UPDATE $db_table_menu SET ITEM_NAME=$item_name, ITEM_DSCRIPTION=$item_desc, ITEM_PRICE=$item_price WHERE ITEM_NO='$item_id'";
	$action_to_take = "remove";
	echo "<br><INPUT type="submit" value="Delete Item"><br>";
  	echo "</form>";
	break;

   case "remove":
	show_data();
	$action_to_take = "remove";
	echo "<br><INPUT type="submit" value="Delete Item"><br>";
  	echo "</form>";
	break;

   default:
	init();
            {{ I NEED TO REFRESH BROWSER HERE }}
	break;
 }


 function create_form() {
 
  global  $item_number, $item_name, $item_desc, $item_price;
 
  ?>
   <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
   <table>
    <tr>
     <td><LABEL for="item_no">Number:</LABEL></td>
     <td><INPUT size="2" type="text" name="item_no" value="<?php echo $item_number ?>"></td>
    </tr>
    <tr>
     <td><LABEL for="item_name">Name:</LABEL></td>
     <td><INPUT type="text" name="item_name" value="<?php echo $item_name ?>"></td>
    </tr>
    <tr>
     <td><LABEL for="item_desc">Description:</LABEL></td>
     <td><INPUT size="100" type="textbox" name="item_desc" value="<?php echo $item_desc ?>"></td>
    </tr>
    <tr>
     <td><LABEL for="item_price">Price:</LABEL></td>
     <td><INPUT size="6" type="text" name="item_price" value="<?php echo $item_price ?>"></td>
    </tr>
   </table>

 
  <?PHP
 }
 
 function show_data() {
 
  global  $item_number, $item_name, $item_desc, $item_price;
  
  ?>

  <br>
  <table align="center" border="1" cellpadding="3" width="90%">
    <tr>
      <td>Current Values</td>
      <td><?PHP echo $item_number ?></td>
      <td><?PHP echo $item_name ?></td>
      <td><?PHP echo $item_desc ?></td>
      <td><?PHP echo $item_price ?></td>
    </tr>
  </table>
  <br><br><br><br>

  <?PHP
 }

 function init() {

  global $db_table_menu, $item_id, $edit, $item_number, $item_name, $item_desc, $row_id, $action_to_take, $item_price;

  $row_id = $_POST['item_id'];
  $action_to_take = $_POST['edit'];
  $query = "SELECT * FROM $db_table_menu WHERE ITEM_NO='$row_id'";
  $display_row = mysql_query($query);
  $row_data = mysql_fetch_array($display_row);
  $item_number = $row_data['ITEM_NO'];
  $item_name = $row_data['ITEM_NAME'];
  $item_desc = $row_data['ITEM_DESCRIPTION'];
  $item_price = $row_data['ITEM_PRICE'];

 }
  ?>
hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/quote]

First of all, there is absolutely no need to set a variable global as you have done in your script, at the top of the page (outside of the functions)  - remove that - it is insecure, it sets all your variables into global scope (bad) .  Now, however , in your functions, there is good reason to set them global , however a better way of writing your functions is to pass the values into teh function *instead* of setting them global . Either way, *unless* a variable is needed inside a function, and isnt passed, dont set it global  

Now, as for your variables being initialized - every page load will do this, if you dont maintain variable state between page loads-

Code: Select all

heade("Location: $_SERVER[PHP_SELF]");
This , while it is one way to redirect to self , would result in a loop that the page will continuously and constantly load - because the switch() is checked first thing on a page load, with no conditionals, doing a header() redirect in the default fall-through condition of the switch is gonna result in an endlessly looping script that refreshes the page as fast as it can be loaded or displayed , and you will never get anything done :)


You need a conditional - for example, check for $_POST or $_GET array, and if it is empty, display the form, if something is set, THEN run the switch statement and handle whatever is passed to it..

Now, however - to maintain the state of the value to the next page, you need to pass it via one of 3 methods that can be used with a redirection - simplest is to pass the single value in the URL as

Code: Select all

header("Location: $_SERVER[PHP_SELF]?passed_value=$pass_this_value");
and then it will show up in the $_GET array on the reload,

Code: Select all

$action_to_take = $_GET['passed_value'];
*HOWEVER* , if for any reason that causes the switch to fall through to the header() call, you end up stuck in yet another endlessly reloading page loop.
Another method is to set session of cookie , but overall, your script's design, from the looks of it, is way off course, and it appears will frequently end up stuck in an endless loop (and you might not even *notice* if you are on a fast connection , except that you might see every time you try to type something in the input box, it goes away hahaha)

Either way, I would say you need to seriously re-think how you are going to handle this script- teh big problem is in that header redirect to itself-

Your best bet is simply to display a form asiking for action to take, rather than header redirect to self.

Posted: Mon Jan 02, 2006 10:18 am
by ToeShot
Thanks for your reply trukfixer. After giving some thought to your response. I have decide to scrap what I was trying to do with the single page (making it the page that does everything). I am going to use seperate pages, to keep it simple for me, until I read some more books and get some more experience in php.

Again Thank You

Posted: Mon Jan 02, 2006 2:46 pm
by mickd
you could also use $_SERVER['QUERY_STRING'] to get the query string

http://au2.php.net/reserved.variables