Jump to content
aveeva

PHP - Ajax Passing value to another php file

Recommended Posts

Posted (edited)

@Barand   For my clarification, this line -> "<td><button class='delbtn' data-id='"+ k +"'>Delete</button></td>"

Edited by aveeva

Share this post


Link to post
Share on other sites
20 hours ago, Barand said:

list = list + "<td><button class='delbtn' data-id='"+ k +"'>Delete</button></td></tr>\n" // add "Delete" button to each playlist item

Was the comment on that line not a sufficient clue?

Share this post


Link to post
Share on other sites

@Barand    That line okay, i got it, no confusion.  My confusion about how can i use in below code,

 

        <?php
       session_start();
      foreach ($_SESSION['playlist'] as $key => $value) {
        echo "<tr>";
        echo "<td>" . $key . "</td>\n<td>" . $value . "</td>";

            //  code....

        echo "</tr>";    
        }
        ?>

 

Share this post


Link to post
Share on other sites

Just echo another <td>..</td> with the delete button html inside it.

Share this post


Link to post
Share on other sites

@Barand   If i ask any stupid question, make me a pardon, i am learning in php.

 

        <?php
       session_start();
      foreach ($_SESSION['playlist'] as $key => $value) {
        echo "<tr>";
        echo "<td>" . $key . "</td>\n<td>" . $value . "</td>";

        echo "<td>" ."<button class='delbtn' data-id='"+ k +"'>Delete</button>" . "<td>";


        echo "</tr>";    
        }

        ?>

Pls correct me?

Share this post


Link to post
Share on other sites

Right idea, but you can't just paste javascript code into php and expect it to work. The variable names and syntax are different.

Share this post


Link to post
Share on other sites
On 7/2/2019 at 12:25 PM, aveeva said:

$statement = $connect->prepare($query);

$statement->execute();

$result = $statement->fetchAll();

$total_row = $statement->rowCount();

$output = '';

In this excerpt from your code, all lines except the execute() are assigning values - you know how to assign in PHP.

Share this post


Link to post
Share on other sites

How is this button going to work?  What is it calling? How is the current row of data being passed to whatever script is going to do the delete?

I suggest a form around each row and a proper Submit button (<input type='submit'>) that will do what you need.  Using the <button> tag is just not that useful even though people love to use it.  I don't get it.

Share this post


Link to post
Share on other sites
27 minutes ago, ginerjm said:

How is this button going to work?  What is it calling? How is the current row of data being passed to whatever script is going to do the delete?

If you read the previous posts and code in this topic before jumping in, all those questions will be answered.

Share this post


Link to post
Share on other sites

@Barand    If i click delete button whole list will be deleted,  how to delete by single,

 

<html>
<head>

    <style type="text/css">
        table {
            border-collapse: collapse;
            border: 1px solid black;
        }
        table td,th {
            border: 1px solid black;
        }
        td {
            text-align: center;
        }
    </style>
</head>

<body>

    <h2>Play Lists</h2>

    <table id="table">
        <th>Voice SKU</th>
        <th>Voice Name</th>
        <th>Action</th>
    

        <?php
       session_start();
      foreach ($_SESSION['playlist'] as $key => $value) {
        echo "<tr>";
        echo "<td>" . $key . "</td>\n<td>" . $value . "</td>";
        echo "<td>". "<button id='btn'>Delete</button>"."</td>";
        echo "</tr>";    
        }

        ?>
    </table>
    <script>

     var btn = document.getElementById('btn');
    btn.onclick = function () {
    document.getElementById('table').remove();
    this.remove();
};
    
        </script>
</body>
</html>

 

Share this post


Link to post
Share on other sites
18 minutes ago, aveeva said:

how to delete by single,

By passing the key of the item you want to delete from the SESSION and not removing the entire table.

Share this post


Link to post
Share on other sites
Posted (edited)

@Barand   Did you mean   ->  document.getElementById('$key').remove(); ?

Edited by aveeva

Share this post


Link to post
Share on other sites

You need to remove them from the SESSION, not just from the page display, then redisplay the list from the session data. Otherwise, when you eventually save the session data the deleted items will still be there.

  • Thanks 1

Share this post


Link to post
Share on other sites
Posted (edited)

help me with my workout :  

my_cart.php: 

    <?php
    header('Content-Type: text/html; charset=utf-8');
    session_start();
    session_regenerate_id();

    echo '
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <metaname="viewport"content="width=device-width,height=device-height,initial-scale=1">
            <title>Playlist</title>
        </head>

        <body>
            <form id="playlist" action="playlist_action.php" method="post">
                <table id="playlist">
                    <caption>Play List</caption>
                    <thead>
                        <tr>
                            <th scope="col">Voice SKU</th>
                            <th scope="col">Voice Name</th>
                            <th scope="col">Action</th>
                        </tr>
                    </thead>

                    <tbody>';
                        foreach ($_SESSION['playlist'] as $key => $value) 
                        {
                            $key = htmlspecialchars($key);
                            echo '
                            <tr>
                                <th scope="row">', $key, '</th>
                                <td>', htmlspecialchars($value), '</td>
                                <td><button name="delete" value="', $key, '">Delete</button></td>
                            </tr>';
                        }
                        echo '
                    </tbody>
                </table>
            </form>
        </body>
    </html>

 

 

playlist_action.php

 

<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
session_regenerate_id();

if (array_key_exists('playlist_action', $_POST)) {
	switch ($_POST['playlist_action']) {
		case 'delete':
			if (array_key_exists($_POST['delete'], $_SESSION['playlist']) {
				unset($_SESSION['playlist'][$_POST['delete']]);
                // notify user of successful delete
			} else {
				// notify user of non-existent key in the playlist
			}	
		default:
			// handle invalid/unknown action here.
	}
}
?>

my output:

|Parse error: syntax error, unexpected 'unset' (T_UNSET) in C:\wamp\www\voice_bank\playlist_action.php on line  *10*|

Edited by aveeva

Share this post


Link to post
Share on other sites

On the line before the "unset" ...

if (array_key_exists($_POST['delete'], $_SESSION['playlist']) {

... count the number of opening parentheses "(" and count the number of closing ones.

(That should show you why the "unset" is unexpected)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

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.