admin管理员组

文章数量:1305424

So, in my website, I have a news system which has an option to edit and delete the news the administrator desires.

Well, I got the edit part right by using:

href="noticiaEditarForm.php?id_noticia=<?php echo $id ?>">Editar</a>

And then a $_GET on the other page.

However, this is not how I desire my editing window. Therefore, I have been exploring a way to send the PHP variable that contains the primary key for the news table (MySQL) to a popup window, using JavaScript. But that's just the thing, it will only return the 1st value it gets from the query... (i.e If I click to edit the 3rd article, it edits my 1st one. Always.)

Here is my current code:

<div class="noticias">
<?php
    include('conn/conn.php');
    mysql_select_db($bd, $conn);

    $resultado = mysql_query("SELECT * FROM noticia INNER JOIN user ON noticia.id_user=user.id_user ORDER BY id_noticia DESC");

    while ($linha = mysql_fetch_array($resultado)) {
        echo "<h1>" . $linha['titulo'] . "</h1>";
        echo "<i>Posted by " .$linha['username']. " on " . "<y>" . $linha['data'] . "</y>" . "</i>";
        echo "<p>";
        echo $linha['texto'];

        $id = $linha['id_noticia'];

        if (isset($_SESSION['admin'])) {
?>
            <div class="noticiasOpcao">
                <a href="" onClick="open_win_editar()">Editar</a>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <a onclick="return confirm('Are you sure?')" href="noticiaApagar.php?id_noticia=<?php echo $id ?>">Apagar</a>
            </div>
<?php
        } 
    }
?>

<script language="javascript">
    function open_win_editar() {
        window.open (
            "noticiaEditarForm.php?id_noticia=<?php echo $id; ?>",
            "Editar notícia",
            "location=1, status=1, scrollbars=1, width=800, height=455"
        );
    }
</script>

<?php mysql_close($conn); ?>

</div>

My point is to then use another query to get the title and text of the article to display on an WYSIWYG editor.

Can anyone point out my flaw?

So, in my website, I have a news system which has an option to edit and delete the news the administrator desires.

Well, I got the edit part right by using:

href="noticiaEditarForm.php?id_noticia=<?php echo $id ?>">Editar</a>

And then a $_GET on the other page.

However, this is not how I desire my editing window. Therefore, I have been exploring a way to send the PHP variable that contains the primary key for the news table (MySQL) to a popup window, using JavaScript. But that's just the thing, it will only return the 1st value it gets from the query... (i.e If I click to edit the 3rd article, it edits my 1st one. Always.)

Here is my current code:

<div class="noticias">
<?php
    include('conn/conn.php');
    mysql_select_db($bd, $conn);

    $resultado = mysql_query("SELECT * FROM noticia INNER JOIN user ON noticia.id_user=user.id_user ORDER BY id_noticia DESC");

    while ($linha = mysql_fetch_array($resultado)) {
        echo "<h1>" . $linha['titulo'] . "</h1>";
        echo "<i>Posted by " .$linha['username']. " on " . "<y>" . $linha['data'] . "</y>" . "</i>";
        echo "<p>";
        echo $linha['texto'];

        $id = $linha['id_noticia'];

        if (isset($_SESSION['admin'])) {
?>
            <div class="noticiasOpcao">
                <a href="" onClick="open_win_editar()">Editar</a>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <a onclick="return confirm('Are you sure?')" href="noticiaApagar.php?id_noticia=<?php echo $id ?>">Apagar</a>
            </div>
<?php
        } 
    }
?>

<script language="javascript">
    function open_win_editar() {
        window.open (
            "noticiaEditarForm.php?id_noticia=<?php echo $id; ?>",
            "Editar notícia",
            "location=1, status=1, scrollbars=1, width=800, height=455"
        );
    }
</script>

<?php mysql_close($conn); ?>

</div>

My point is to then use another query to get the title and text of the article to display on an WYSIWYG editor.

Can anyone point out my flaw?

Share Improve this question edited Oct 3, 2016 at 13:17 rocambille 16k12 gold badges52 silver badges70 bronze badges asked Apr 20, 2012 at 16:00 Ricardo JerónimoRicardo Jerónimo 1131 gold badge4 silver badges13 bronze badges 5
  • What is the actual rendered output? Is the $id the same on each iteration of the loop? – David Commented Apr 20, 2012 at 16:05
  • Also, the window.open JavaScript call will always have the last $id from the PHP loop, since it happens after the loop. So that function will always open the last one. – David Commented Apr 20, 2012 at 16:06
  • did you try to load the "edit window" directly with correct id? Where is your query that loads the record by id? – Alex Commented Apr 20, 2012 at 16:07
  • David: I think that's the issue here. He needs to fill out a script block or something to store the possible ID's rather than hardcoding the ID in the open_win_editar function. – user1086498 Commented Apr 20, 2012 at 16:07
  • Ricardo, you do not need to edit the title and add "Resolved", just press the accept button next to an answer (like you've already done). – Lekensteyn Commented Apr 20, 2012 at 16:30
Add a ment  | 

3 Answers 3

Reset to default 3

This code:

<script language="javascript">
    function open_win_editar() {
        window.open ("noticiaEditarForm.php?id_noticia=<?php echo $id; ?>", "Editar notícia", "location=1, status=1, scrollbars=1, width=800, height=455");
     }
</script>

Is happening outside of the PHP while loop, so the value of $id will be the last value that was set to $id in the loop. So the JavaScript code will always open the same link.

If you need the code within the PHP loop to specify the $id value for the JavaScript, then you can pass it as an argument to the JavaScript function. Something like this:

<script language="javascript">
    function open_win_editar(targetID) {
        window.open ("noticiaEditarForm.php?id_noticia=" + targetID, "Editar notícia", "location=1, status=1, scrollbars=1, width=800, height=455");
     }
</script>

So the code rendering the anchor tags in the loop would pass the argument like this:

<a href="" onClick="open_win_editar(<?php echo $id; ?>)">Editar</a>

The rendered output would then contain the record-specific $id value on each a tag to be used by the JavaScript code on the client.

<script language="javascript">
            function open_win_editar() {
                window.open ("noticiaEditarForm.php?id_noticia=<?php echo $id; ?>", "Editar notícia", "location=1, status=1, scrollbars=1, width=800, height=455");
            }
        </script>

This is only rendered once, probably with the last ID. You need to figure out a structure so you can pass a different ID to it based on what article you clicked to edit.

The id of the piece to edit is only updated within the while loop and never outside of it.

To use it as you wish you should use a parameter for the open_with_editar function:

<script language="javascript">
            function open_win_editar(id) {
                window.open ("noticiaEditarForm.php?id_noticia="+id, "Editar notícia", "location=1, status=1, scrollbars=1, width=800, height=455");
            }
        </script>

Now you only have to update the onclick event and hand over teh respective id:

<div class="noticiasOpcao">
      <a href="" onClick="open_win_editar(<?php echo $id; ?>)">Editar</a>

That should work.

Regards STEFAN

本文标签: Javascript and PHP (windowopen)Stack Overflow