My web host recently updated their version of PHP and some of my code is no longer working. I created a band website that stores news items in a MySQL database. I created an update page (news.php) where the band can log in and add, update, or delete news. I have the following section of code in news.php which displayes news items from the table into a drop down list and calls a javascript function to pop up the news item in another window:

<form name="updateNews" method="post" onSubmit="updateWindow(this.updateNews.value)" >
  <tr>
    <td width="17%"><div align="right"><strong>Select news to Update:</strong></div></td>
    <td width="1%">&nbsp;</td>
    <td width="82%"><div align="left">
    <select name="updateNews" class="select">
    <?php
      do 
      {  
    ?>
    <option value="<?php echo $row_NewsUpdate['seq_id']?>"><?php echo $row_NewsUpdate['news_date']?></option>
    <?php
      } 
      while ($row_NewsUpdate = mysql_fetch_assoc($NewsUpdate));
      $rows = mysql_num_rows($NewsUpdate);
      if($rows > 0) 
      {
        mysql_data_seek($NewsUpdate, 0);
        $row_NewsUpdate = mysql_fetch_assoc($NewsUpdate);
      }
    ?>
        </select>
        </div>
        </td>
  </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><input name="update" type="submit" class="buttons" id="update" value="Get News to Update" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
</form>

I have the following code to select from the database (I put xxxxx in place of real names):

mysql_select_db($database_xxxxxx, $xxxxxx);
$query_NewsUpdate = "select seq_id,news_date,news_content from news order by news_date DESC";
$NewsUpdate = mysql_query($query_NewsUpdate, $xxxxxx) or die(mysql_error());
$row_NewsUpdate = mysql_fetch_assoc($NewsUpdate);
$totalRows_NewsUpdate = mysql_num_rows($NewsUpdate);

$query_NewsDelete = "select seq_id,news_date,news_content from news order by news_date DESC";
$NewsDelete = mysql_query($query_NewsDelete, $xxxxxx) or die(mysql_error());
$row_NewsDelete = mysql_fetch_assoc($NewsDelete);
$totalRows_NewsDelete = mysql_num_rows($NewsDelete);
?>

Here is the Javascript function I'm using:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="JavaScript">
<!--
function updateWindow(seq_id)
{  
  newsUpdateWindow = window.open("updateNews.php?seq_id="+seq_id,"newsUpdate_window","height=500,width=600,scrollbars");
}
function deleteWindow(seq_id)
{  
  newsDeleteWindow = window.open("deleteNews.php?seq_id="+seq_id,"newsDelete_window","height=500,width=600,scrollbars");
}

//-->
</script> 

A couple of things I checked:

  1. news.php is receiving the seq_id and is populating the drop down list with the correct values.
  2. I put print statements in update.php to see if it was receiving the seq_id from news.php and it is not.

This code worked great until php was updated on the server. I'm not sure why seq_id is no longer being passed to update.php.

Any thoughts on what may be happening? I really appreciate any help.

Dani AI

Generated

Most likely cause: the site relied on implicitly-created global variables that stopped appearing after the PHP upgrade (register_globals and related behavior changed between PHP4 and later PHP releases). That explains why the select is populated in news.php but the target page never sees a bare $seq_id. and were pointing in the right direction — you must read input from the superglobals on the server instead of expecting automatic global variables. (wiki.php.net)

Client-side checks and a safe quick fix:

  • Verify the JavaScript actually receives the select value (use console.log() or alert() inside the click handler).
  • Avoid naming the form and the select the same; give the select an id like seq_id.
  • Prefer a button with type="button" that calls a function which builds a properly encoded URL and opens the popup. Example pattern (keeps the popup tied to a user gesture and encodes the value):
<input type="button" value="Get News to Update" onclick="openUpdate()" />

<script>
function openUpdate(){
  var seq = document.getElementById('seq_id').value;
  if(!seq) return alert('Select an item');
  window.open('updateNews.php?seq_id=' + encodeURIComponent(seq),
              'newsUpdate_window','height=500,width=600,scrollbars=1');
}
</script>

Use encodeURIComponent() so special characters won't break the query string, and check window.open() return (it will be null if blocked). (developer.mozilla.org)

Server-side: read and validate the GET parameter explicitly (do not rely on $seq_id existing). Example approach using a filter:

$seq = filter_input(INPUT_GET, 'seq_id', FILTER_VALIDATE_INT);
if ($seq === false || $seq === null) { /* handle missing/invalid id */ }
// then fetch the record using a prepared query (PDO or mysqli prepared statements)

$_GET/filter_input are the supported ways to read query strings; validate before using. (php.net)

Extra troubleshooting: confirm you put your debug prints in the exact file opened by the popup (JS opens updateNews.php in the sample). Also check browser console for JS errors, and remember onsubmit handlers must call event.preventDefault() (or return false properly) if you want to stop the normal form POST while opening a window. Using the pattern above (type="button") avoids the submit/POST race entirely. (developer.mozilla.org)

If these steps still fail, the next things to mention: verify you aren’t testing the wrong PHP filename (update.php vs updateNews.php), and inspect the popup’s address bar to see whether ?seq_id=... was actually appended — that will immediately show whether the problem is client- or server-side.

Recommended Answers

All 6 Replies

Member Avatar for Member #334542

Try to use directly the echo statement with your javascript code

<?php echo $seq_id;?>

from php version ?? to php version ??
the php.net website has a long list of details on the upgrade from one version of php to the next, lilkely you can go there and find details on what syntax is changed
often an variable that was able to be accessed as $variable has been changed so that it is accessed as $_server['variable'] $_get['variable'] or some other simple change
without knowing the two versions of php incvolved we are all shooting in the dark

when submitting code wrap it in [code=language] code [/code] tags where 'language' is php|html|css|perl|javascript|sql (any language) and the code will be presented appropriately highlighted,
often people who know the solution, wont bother if they have to wade through plain text

I put print statements in update.php to see if it was receiving the seq_id from news.php and it is not.

most likely this is the "register global" problem. You should edit your code as Almostbob recommend, using $_POST[] etc.

Sorry about not wrapping the code in tags - I will make sure to do that in the future. This was my first post.

PHP was upgraded from php4 to php5. The select queries (which populate the option_value) are working fine. I believe the root is either the variable I'm trying to pass from option_value to the Javascript pop_up window function isn't being received or the javascript function isn't passing the variable successfully to the next php page to bring up the specific record. I'm not sure how to test if the Javascript function is receiving the variable being passed from the option_value.

Obviously, I don't have much experience in this area. It's been about 5 years since I wrote the original code and haven't really worked with PHP or Javascript since.

Any direction you can give is appreciated. Thanks!

from php version ?? to php version ??
the php.net website has a long list of details on the upgrade from one version of php to the next, lilkely you can go there and find details on what syntax is changed
often an variable that was able to be accessed as $variable has been changed so that it is accessed as $_server['variable'] $_get['variable'] or some other simple change
without knowing the two versions of php incvolved we are all shooting in the dark

when submitting code wrap it in [code=language] code [/code] tags where 'language' is php|html|css|perl|javascript|sql (any language) and the code will be presented appropriately highlighted,
often people who know the solution, wont bother if they have to wade through plain text

If anyone thought there was anything other than newness as the source,
ya wouldn' believe how **expletive deleted** some of the replies would be,

maybe this

<?php
do
{
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.