www.webdeveloper.com
Results 1 to 7 of 7

Thread: I need a PHP function to run on the click of a button..

  1. #1
    Join Date
    Aug 2012
    Posts
    23

    I need a PHP function to run on the click of a button..

    I had to redesign my blog cms with PHP due to JavaScripts inability to save changes to xml files. I need to make a delete function run when you click the delete button. It will delete the specified blog post.

    Here is my code so far.

    <?

    $doc = new DOMDocument();
    $doc->load("news.xml");

    $channel = $doc->getElementsByTagName("channel");
    $channel = $channel->item(0);

    $items = $doc->getElementsByTagName("item");
    $titles = $doc->getElementsByTagName("title");
    $links = $doc->getElementsByTagName("link");
    $descriptions = $doc->getElementsByTagName("description");

    for($i = 0; $i < $items->length; $i++)
    {
    $title = simplexml_import_dom($titles->item($i));
    $link = simplexml_import_dom($links->item($i));
    $description = simplexml_import_dom($descriptions->item($i));

    echo "<h6><a href=\"" . $link . "\">" . $title . "</a></h6>";
    echo "<p id=\"text\">" . $description . "</p>";
    echo "<input type=\"button\" value=\"delete\">";
    }

    ?>

    I know i cannot use the action attribute inside of a form because i cannot send the $i variable into the function that way. i need to somehow declare the function during the for loop so that way when i click the button it will execute on a specific value that lived in the $i variable at that time.

    Here is the delete blog post code

    function removePost($i)
    {
    $channel->removeChild("$items->item($);

    echo "The specified blog post was deleted!";
    }

  2. #2
    Join Date
    Jan 2004
    Posts
    484
    inorder to do what you want your delete button will need to hold the node value
    $i and also need to be type submit

    ie.
    PHP Code:
    <input type='submit' value='delete[$i]' name='Delete' /> 
    each submit will need to be wrapped in it's own form

    the other way you can achieve this is via href

    ie.
    PHP Code:
    <a href='processing-page.php?action=delete&value'.$i >Delete $i</a

  3. #3
    Join Date
    Aug 2012
    Posts
    23
    So the following would work?

    <input type="submit" value="the_nodes_name[$i]" name="Delete" />

    how would i use the value attribute in the php code?

  4. #4
    Join Date
    Aug 2012
    Posts
    23
    Can somebody clarify how i do what natdrip was talking about?

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    If you take a look at _GET variables I think the examples there should help clarify for you.

  6. #6
    Join Date
    Aug 2012
    Posts
    23
    i see so if i use an array in this manner of setting the value to delet[$i] it will return only the ones that are clicked on?

  7. #7
    Join Date
    Aug 2012
    Posts
    23
    I understand now. Figured it out the day after i last posted but i want to clarify for those who might search this thread for future reference.

    When using:

    Link Click
    <a href="delete.php?action=delete&value"></a> you are sending the value of the delete variable to the delete.php script. This allows you to retrieve the value with the $_GET superglobal array.

    When using:

    Button Click
    <button type="submit" name="delete[{$i}]">delete</button> you are sending the value $i to the script delete.php or what ever script is in the forms action attribute. You can then retrieve the value with the $_GET superglobal array.

    Note: Make sure to use a <button> element instead of an <input> element. this allows you to have the button say delete without it printing the value held in $i as show in Natdrips post. Its slightly cleaner.

    Thank you Natdrip for showing me how this works. I appreciate it a lot!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles