Newbie question

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

PieceOfMeat
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Sep 21, 2012 12:20 pm

Newbie question

Postby PieceOfMeat » Fri Sep 21, 2012 12:28 pm

Im trying to build a phonebook in a table shape. Every row in the table represent a person and in the end of
each row there is a trash can.
I want that when the user clicks on a trash can it deletes the person in the same row of the trash can.
My question is how do i build a listener in php file that can recognize which trash can been clicked?

kyle04
New php-forum User
New php-forum User
Posts: 94
Joined: Sat Jul 07, 2012 1:36 pm

Re: Newbie question

Postby kyle04 » Fri Sep 21, 2012 3:35 pm

You have to code each trash can, ie link, to tie in with the table value (ideally the PRIKEY or primary key).
Assuming you are storing the data in a database table and building your display table from that :

...........
while($row = mysql_fetch_array($result)) {
........
$key = $row['PRIKEY'];
echo "<a href=\"myscript.php?action=delete&delete_key=$key\"><img src=\"trashcan.jpg\"><\a>";

}

if($action=="delete") {
$key = $_REQUEST['delete_key'];
mysql_query("DELETE FROM TABLENAME WHERE PRIKEY='$key'");
echo "<script type=\"text/javascript\">";
echo "alert('record deleted');
window.location='myscript.php';";
echo "</script>";
}

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Newbie question

Postby faust » Fri Sep 21, 2012 11:35 pm

I agree with kyle04 but the only thing that I think everybody should avoid doing is echo-ing html content. It's harder to manage, to escape and in general it's not a good practice. There's nothing wrong with closing and opening the <?php ?> tags where ever a html content needs to be echo-ed

PieceOfMeat
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Sep 21, 2012 12:20 pm

Re: Newbie question

Postby PieceOfMeat » Sat Sep 22, 2012 2:33 am

kyle04 wrote:You have to code each trash can, ie link, to tie in with the table value (ideally the PRIKEY or primary key).
Assuming you are storing the data in a database table and building your display table from that :

...........
while($row = mysql_fetch_array($result)) {
........
$key = $row['PRIKEY'];
echo "<a href=\"myscript.php?action=delete&delete_key=$key\"><img src=\"trashcan.jpg\"><\a>";

}

if($action=="delete") {
$key = $_REQUEST['delete_key'];
mysql_query("DELETE FROM TABLENAME WHERE PRIKEY='$key'");
echo "<script type=\"text/javascript\">";
echo "alert('record deleted');
window.location='myscript.php';";
echo "</script>";
}


Thnx for your help but i have some problems:
1) "Notice: Undefined variable: action"
2) I need all my javascript to be in external file.
3) I replace all the myscript.php in the name of the php file that i put the above code in. Is that what should i do? Because when i click the trash can it says that "page not found Http 404".

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Newbie question

Postby faust » Sat Sep 22, 2012 3:38 am

I think you didn't quite understand what kyle04 told you. I'll try to summarize it for you.
1) Create a file myscript.php;
2) Get the data from your db (I guess you already did that);
3) When listing the results I think you are doing something like this:

Code: Select all

while($row = mysql_fetch_array($result)) {
    //........
    //echo everything
    //........

    //this part here is the link for the delete action
    $key = $row['PRIKEY'];
    echo "<a href=\"myscript.php?action=delete&delete_key=$key\"><img src=\"trashcan.jpg\"><\a>";
}

4) At the beginning of your myscript.php file place this code (before you list your items):

Code: Select all

if ($_GET['action'] == 'delete')
{
    $key = $_GET['delete_key'];
    mysql_query("DELETE FROM <YOUR-TABLENAME-HERE> WHERE PRIKEY='$key'");
}


This should do the trick. Personally I don't think it's a good idea to echo html within the php code especially <script> tags and js. It's a really bad practice. Try it and tell how it went. Cheers :)

PieceOfMeat
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Sep 21, 2012 12:20 pm

Re: Newbie question

Postby PieceOfMeat » Sat Sep 22, 2012 4:11 am

Thank you fause but im still getting the "Notice: Undefined variable: action". Anyway, it doesn't looks logic to me to put the:
if (! $result) {
echo '<p> Unable to perform the SELECT query</p>';}
if($action=="delete") {
$key = $_REQUEST['delete_key'];
mysql_query("DELETE FROM avi WHERE id='$key'");
before listing the items. Because when im listing the items i define action.
And why inside the 'if' the name is $action and when i define action it's without the $?
By the way, now i have two more questions. 1)how can i copy a variable value from php file to another php file? i know how to do it by clicking submit button using:
<a href = "nextpage.php?name=<?php echo urlencode($_GET['name']); ?>">
But how can i do that automatically without needing to click anything?
2) When the main page of my websites named index.html, it goes to that page when i enter my website
automatically. If i cahnge it to index.php, would it still be the default page?
Thnx again :)

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Newbie question

Postby faust » Sat Sep 22, 2012 6:56 am

1st I didn't tell you to write if($action=="delete") {. It should be if ($_GET['action'] == 'delete') {. Don't read your code line by line. Think of what will be executed when. I think you should really read more about superglobal variables. I'll try to summarize $_GET and $_POST for you.
You see kyle04 told you to make the link like that: myscript.php?action=delete&delete_key={$key}. When you click the link your browser is redirected to myscript.php and 'action' and 'delete_key' are send as $_GET parameters. You can then read them with $_GET['action'] and $_GET['delete_key'].
$_POST is similar, but the variables are not send via URL address, but by submitting a form. For example, you have a form with an input text field with name="text_field". When you press the submit button, the browser is redirected to the url written in the form action="" parameter. Then in that page you can read $_POST['text_field'].
$_GET and $_POST are nothing more that 2 arrays that are available globally. kyle04 made a mistake by writing $action. $action means a VARIABLE with name action. $_GET['action'] means that you are accessing the element with key 'action' from the array $_GET.

So with this explained - when you click the link:

Code: Select all

<a href="myscript.php?action=delete&delete_key={$key}">Delete Me</a>

The browser refreshes, opens the myscript.php file and in the beginning of that file you can check the value of $_GET['action']. If you don't have ?action=delete in your url address, the IF statement won't be true.
I didn't understand your first question.
Yes, if you rename the file to index.php it will be executed the same as index.html

PieceOfMeat
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Sep 21, 2012 12:20 pm

Re: Newbie question

Postby PieceOfMeat » Sat Sep 22, 2012 7:59 am

I did what you say and now on the line:
if($_GET['action'] == 'delete')
It tells me:
"Notice: Undefined index: action "
Here my script:
<?php
$sql = "SELECT id, first, last, mail, number FROM mytable";
$result = @mysql_query($sql);
if (! $result) {
echo '<p> Unable to perform the SELECT query</p>';}
if($_GET['action'] == 'delete') {
$key = $_GET['delete_key'];
mysql_query("DELETE FROM my table WHERE id='$key'");
}
while ($row = mysql_fetch_array($result)) {
$key = $row['id'];
if ($key%2==0){
echo '<tr class="light"><td>'.$row['first'].'</td><td>'. $row['last'].'</td><td class="email">'.$row['mail'].'</td><td>'. $row['number'].'</td><td name="edit">Edit</td>
<td><a href=\"login_success.php?action=delete&delete_key=$key\"><img src="light.JPG" alt="Delete"></a></td></tr>';
}
else{
echo '<tr class="papaya"><td>'.$row['first'].'</td><td>'. $row['last'].'</td><td class="email">'.$row['mail'].'</td><td>'. $row['number'].'</td><td name="edit">Edit</td>
<td><a href=\"login_success.php?action=delete&delete_key=$key\"><img src="papaya.JPG" alt="Delete"></a></td></tr>';
}
}
?>
What i did wrong?

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Newbie question

Postby faust » Sat Sep 22, 2012 8:55 am

You forgot to check if $_GET['action'] isset. I rearranged your code. Now it looks tidier ^_^

Code: Select all

<?php
if(isset($_GET['action']) && $_GET['action'] == 'delete')
{
   $key = $_GET['delete_key'];
   mysql_query("DELETE FROM my table WHERE id='$key'");
}

$sql = "SELECT id, first, last, mail, number FROM mytable";
$result = @mysql_query($sql);
if (!$result)
{
?>

<p> Unable to perform the SELECT query</p>

<?php
   while ($row = mysql_fetch_array($result))
   {
      $key = $row['id'];
      if ($key%2 == 0)
      {
?>
         <tr class="light">
            <td><?php echo $row['first']; ?></td>
            <td><?php echo $row['last']; ?></td>
            <td class="email"><?php echo $row['mail']; ?></td>
            <td><?php echo $row['number']; ?></td>
            <td name="edit">Edit</td>
            <td><a href="login_success.php?action=delete&delete_key=<?php echo $key; ?>"><img src="light.JPG" alt="Delete"></a></td>
         </tr>
<?php
      }
      else
      {
?>
         <tr class="papaya">
            <td><?php echo $row['first']; ?></td>
            <td><?php echo $row['last']; ?></td>
            <td class="email"><?php echo $row['mail']; ?></td>
            <td><?php echo $row['number']; ?></td>
            <td name="edit">Edit</td>
            <td><a href="login_success.php?action=delete&delete_key=<?php echo $key; ?>"><img src="papaya.JPG" alt="Delete"></a></td>
         </tr>   
<?php
      }
   } //end while
} //end if
?>


Don't echo so much html code especially on one row... Do you see how better and more readable it looks now?

kyle04
New php-forum User
New php-forum User
Posts: 94
Joined: Sat Jul 07, 2012 1:36 pm

Re: Newbie question

Postby kyle04 » Sat Sep 22, 2012 2:45 pm

Good work faust - my snippet was merely a guide !
Perhaps I should have suggested at the beginning :

foreach($_REQUEST as $name=>$value) { // or $_GET
${$name} = $value;
}

PieceOfMeat
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Sep 21, 2012 12:20 pm

Re: Newbie question

Postby PieceOfMeat » Sat Sep 22, 2012 3:39 pm

Thnx man you saved me. Your code didnt realy work, it write "Unable to perform the SELECT query"
and a warning about a boolean in the line of the "while".
So i did a combination of you last code and the previous code and it looks like this:
<?php
$counter=1;
$sql = "SELECT id, first, last, mail, number FROM avi";
$result = @mysql_query($sql);
if (! $result) {
echo '<p> Unable to perform the SELECT query</p>';}
if(isset($_GET['action']) && $_GET['action'] == 'delete') {
$key = $_GET['delete_key'];
mysql_query("DELETE FROM avi WHERE id='$key'");
}
while ($row = mysql_fetch_array($result)) {
$key = $row['id'];
if ($counter%2==0){
echo '<tr class="light"><td>'.$row['first'].'</td><td>'. $row['last'].'</td><td class="email">'.$row['mail'].'</td><td>'. $row['number'].'</td><td name="edit">Edit</td>
<td>';?>
<a href="login_success.php?action=delete&delete_key=<?php echo $key; ?>"><img src="light.JPG" alt="Delete"></a></td></tr>
<?php
}
else{
echo' <tr class="papaya"><td>'.$row['first'].'</td><td>'. $row['last'].'</td><td class="email">'.$row['mail'].'</td><td>'. $row['number'].'</td><td name="edit">Edit</td>
<td>';?>
<a href="login_success.php?action=delete&delete_key=<?php echo $key; ?>"><img src="light.JPG" alt="Delete"></a></td></tr>
<?php
}
$counter++;
}
?>

It works great!!!! :D
So i left only with the question that you didnt understand so ill ask it differently.
On my index page the user types the user name and password. By using $_post in checklogin.php i validate that the username and password are good. From checklogin.php i automatically move to login_successs.php. But how i can tell in login_successs who is the user that i validated in checklogin.php
???

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Newbie question

Postby johnj » Sun Sep 23, 2012 5:42 am

...But how i can tell in login_successs who is the user that i validated in checklogin.php


For that, you can either use a session variable OR use a form variable that is hidden.

PieceOfMeat
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Sep 21, 2012 12:20 pm

Re: Newbie question

Postby PieceOfMeat » Sun Sep 23, 2012 9:07 am

johnj wrote:
...But how i can tell in login_successs who is the user that i validated in checklogin.php


For that, you can either use a session variable OR use a form variable that is hidden.


Can you please give me an example?

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Newbie question

Postby faust » Sun Sep 23, 2012 11:54 am


PieceOfMeat
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Sep 21, 2012 12:20 pm

Re: Newbie question

Postby PieceOfMeat » Wed Sep 26, 2012 1:45 pm

It's a great example. Thnx!!! :)

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Newbie question

Postby faust » Wed Sep 26, 2012 10:45 pm

Glad we could help ^_^


Return to “PHP coding => General”

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest