• hello,
    i want to make a plugin that has add,delete or edit buttons to enable me doing this functions on any row in a table then this changes saved in my database .
    i’ll be grateful if anyone help me in that matter
    thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • What table? A database table? Why don’t you want to use phpMyAdmin or something similar instead? Can you elaborate on what you’re trying to do?

    Thread Starter ola-hashem

    (@ola-hashem)

    i want to create an admin plugin that contain a HTML table and some buttons such as add button to add a new row in that table or edit a row or delete row and the values of that table are saved in a database table also the changes we make .

    How far did you get? What exactly isn’t working?

    Thread Starter ola-hashem

    (@ola-hashem)

    all of them not working “add,delete and edit” .
    the main problem i face is when i put the pages that make this functions”add,delete and edit” in the same folder with my plugin file, nothing work.

    What error do you get? Can you post a snippet of the corresponding code?

    Thread Starter ola-hashem

    (@ola-hashem)

    this my code and i changed in it by writing javascript function to add row in my table but the save button not working.
    <?php

    /*
    Plugin Name: Admin Page
    Description: sample plugin
    Plugin URI: http://www.hotmail.com
    */

    function plugin_menu2()
    {
    add_options_page(“WP Plugin”, “WP select a link”, 10, “wp_plugin”, “wp_back_end”);
    //add_submenu_page(‘post.php’, ‘My Plugin Managment’, ‘My Plugin Managment’, 8, ‘myplugin/myplugin_admin.php’);
    }

    function wp_back_end()
    {

    mysql_connect(“localhost”,”root”,””);
    mysql_select_db(“wordpress”);
    $sel = “select * from url”;
    // where title = ‘”.$_POST[‘title1’].”‘ and url = ‘”.$_POST[‘url1’].”‘” ;
    $result=mysql_query($sel);
    $n=mysql_num_rows($result);
    ?>
    <form method=”post” action=”<?php print $_SERVER[PHP_SELF] ?>”>
    <table align=”center” border=”2″ bgcolor=”#FFFFFF” id=”table1″>
    <tbody>
    <tr bgcolor=”#66CCFF”>
    <th> Title</th>
    <th> URL Link</th>
    <th></th>
    <th></th>

    </tr>
    <?
    for($i=0;$i<$n;$i++)
    {
    $row = mysql_fetch_array($result);
    ?>
    <tr>
    <td><input type=”text” name=”title1″ value=”<? echo $row[0] ?>”></td>
    <td> <input type=”text” name=”url1″ value=”<? echo $row[1] ?>”></td>
    <td>“>Edit</td>
    <td>“>Delete</td>
    </tr>

    <?
    } //end-of-for
    ?>

    </tbody>
    </table>
    <input type=”button” value=”Add Row” align=”absmiddle” name=”btn” onclick=”addRow(‘table1’);”>

    </form>

    <script language=”javascript”>

    var count = “1”;
    function addRow(table1)
    {
    var form = document.createElement(“form”);

    var tbody = document.getElementById(table1).getElementsByTagName(“TBODY”)[0];
    // create row
    var row = document.createElement(“TR”);

    // create table cell 1 —– title
    var td1= document.createElement(“TD”)
    var strHtml1= “<INPUT TYPE=\”text\” NAME=\”title2\”>”;
    td1.innerHTML = strHtml1.replace(/!count!/g,count);

    // create table cell 2 —– url
    var td2 = document.createElement(“TD”)
    var strHtml2 = “<INPUT TYPE=\”text\” NAME=\”url2\”>”;
    td2.innerHTML = strHtml2.replace(/!count!/g,count);

    //create table cell 3 —-save
    var td3 = document.createElement(“TD”)
    var strHtml3 = “<INPUT TYPE=\”submit\” VALUE=\”Save\”>”;
    td3.innerHTML = strHtml3.replace(/!count!/g,count);

    var currentElement = document.createElement(“input”);
    currentElement.setAttribute(“type”, “hidden”);
    currentElement.setAttribute(“name”, “hiddenName”);
    currentElement.setAttribute(“id”, “hiddenName”);
    currentElement.setAttribute(“value”, “insertrow”);
    form.appendChild(currentElement);

    // create table cell 4 —–edit
    // var td4 = document.createElement(“TD”)
    //var strHtml4 = “<INPUT TYPE=\”Button\” VALUE=\”Edit\”>”;
    //td4.innerHTML = strHtml4.replace(/!count!/g,count);

    // create table cell 5 —–delete
    //var td5 = document.createElement(“TD”)
    //var strHtml5 = “<INPUT TYPE=\”Button\” VALUE=\”Delete\”>”;
    //td5.innerHTML = strHtml5.replace(/!count!/g,count);
    // append data to row
    row.appendChild(td1);
    row.appendChild(td2);
    row.appendChild(td3);
    //row.appendChild(td4);
    //row.appendChild(td5);

    // add to count variable
    count = parseInt(count) + 1;
    // append row to table
    tbody.appendChild(row);

    } //end-function-add

    </script>
    <?
    if (isset($_POST[‘insertrow’]))
    {
    include(“my_config.php”);
    $insert = “insert into url (title , url) values (‘”.$_POST[‘title2’].”‘,'”.$_POST[‘url2’].”‘)”;
    mysql_query($insert);
    echo “Your record has inserted into db” ;
    } //end-if
    } //end-of-function

    add_action(“admin_menu”, “plugin_menu2”);
    ?>
    thanks

    There’re various problems. Let’s look at them in more detail:

    1. Your form sets its action attribute to “$_SERVER[PHP_SELF]“. This isn’t a good idea because WordPress determines the current options page via the query parameter page; like “?page=my_plugin” in the URL. It’s better to just set action to an empty string.
    2. The form created in the JavaScript is never added to the DOM, hence it has no effect. Just remove “var form ...” and the like.
    3. The if statement “if (isset($_POST['insertrow']))” never evaluates to true because there’s no parameter named insertrow in the post data. Although you’ve added an input field with this value to the form that never gets added to the DOM you will have to change the if statement to something like this “if (isset($_POST['hiddenName']) and $_POST['hiddenName'] == 'insertrow')“. This checks the presence of a parameter named hiddenName and compares its value to insertrow.
    4. The if statement still doesn’t evaluate to true because there’s no parameter hiddenName in the post data. Just add one after the “Save” button like so: “var strHtml3 = "<INPUT TYPE=\"submit\" VALUE=\"Save\"><input type=\"hidden\" name=\"hiddenName\" id=\"hiddenName\" value=\"insertrow\">";“.

    I’d like to note that your code is a real mess. Try to clean it up a bit; using $wpdb instead of calling the MySQL functions directly would be a good start.

    Thread Starter ola-hashem

    (@ola-hashem)

    thanks alot for your help, i followed the points you said and it works .

    Thread Starter ola-hashem

    (@ola-hashem)

    i have another problem in this code that the delete button not working.
    when clicking on it, the row is deleted from the admin plugin but this changes not saved in my db (this row isn’t deleted from db)
    and this is the code ::
    <?php

    /*
    Plugin Name: Admin Page
    Description: sample plugin
    Plugin URI: http://www.hotmail.com
    */

    function plugin_menu2()
    {
    add_options_page(“WP Plugin”, “WP select a link”, 10, “wp_plugin”, “wp_back_end”);
    }

    function wp_back_end()
    {

    mysql_connect(“localhost”,”root”,””);
    mysql_select_db(“wordpress”);
    $sel = “select * from url”;
    $result=mysql_query($sel);
    $n=mysql_num_rows($result);
    ?>
    <form method=”post” action=””>
    <table align=”center” border=”2″ bgcolor=”#FFFFFF” id=”table1″>
    <tbody>
    <tr bgcolor=”#66CCFF”>
    <th> Title</th>
    <th> URL Link</th>
    <th></th>
    <th></th>

    </tr>
    <?
    for($i=0;$i<$n;$i++)
    {
    $row = mysql_fetch_array($result);

    ?>
    <tr>
    <td><input type=”text” name=”title1″ value=”<? echo $row[0] ?>”></td>
    <td> <input type=”text” name=”url1″ value=”<? echo $row[1] ?>”></td>
    <td><input type=”button” value=”Delete” name=”del” onclick=”delRow();”>
    <input type=”hidden” name=”h1″ value=”deleterow”></td>

    </tr>

    <?
    } //end-of-for
    ?>

    </tbody>
    </table>

    <input type=”button” value=”Add Row” align=”absmiddle” name=”btn” onclick=”addRow(‘table1’);”>

    </form>

    <script language=”javascript”>

    var count = “1”;
    function addRow(table1)
    {
    form = document.createElement(“form”);

    var tbody = document.getElementById(table1).getElementsByTagName(“TBODY”)[0];
    // create row
    var row = document.createElement(“TR”);

    // create table cell 1 —– title
    var td1= document.createElement(“TD”)
    var strHtml1= “<INPUT TYPE=\”text\” NAME=\”title2\”>”;
    td1.innerHTML = strHtml1.replace(/!count!/g,count);

    // create table cell 2 —– url
    var td2 = document.createElement(“TD”)
    var strHtml2 = “<INPUT TYPE=\”text\” NAME=\”url2\”>”;
    td2.innerHTML = strHtml2.replace(/!count!/g,count);

    //create table cell 3 —-save
    var td3 = document.createElement(“TD”)
    var strHtml3 = “<INPUT TYPE=\”submit\” VALUE=\”Save\”><input type=\”hidden\” name=\”hiddenName\” id=\”hiddenName\” value=\”insertrow\”>”;
    td3.innerHTML = strHtml3.replace(/!count!/g,count);

    var currentElement = document.createElement(“input”);
    currentElement.setAttribute(“type”, “hidden”);
    currentElement.setAttribute(“name”, “hiddenName”);
    //currentElement.setAttribute(“id”, “hiddenName”);
    currentElement.setAttribute(“value”, “insertrow”);
    form.appendChild(currentElement);

    // append data to row
    row.appendChild(td1);
    row.appendChild(td2);
    row.appendChild(td3);
    //row.appendChild(td4);
    //row.appendChild(td5);

    // add to count variable
    count = parseInt(count) + 1;
    // append row to table
    tbody.appendChild(row);

    } //end-function-add

    function delRow()
    {
    var current = window.event.srcElement;
    //here we will delete the line
    while ( (current = current.parentElement) && current.tagName !=”TR”);
    current.parentElement.removeChild(current);
    }

    </script>
    <?
    if (isset($_POST[‘hiddenName’]) and $_POST[‘hiddenName’] == ‘insertrow’)
    {
    include(“my_config.php”);
    $insert = “insert into url (title , url) values (‘”.$_POST[‘title2’].”‘,'”.$_POST[‘url2’].”‘)”;
    mysql_query($insert);
    //echo “Your record has inserted into db” ;
    } //end-if-insert

    else if (isset($_POST[‘h1’]) and $_POST[‘h1’] == ‘deleterow’)
    {
    include(“my_config.php”);
    $del= “delete from url
    where title='”.$_POST[‘del’].”‘”;
    //echo $del;
    $result = mysql_query($del);
    echo “<script>
    alert(‘the specified row is deleted from db’)
    </script>”;
    } //end-if-delete

    } //end-of-function

    add_action(“admin_menu”, “plugin_menu2”);
    ?>
    thanks

    Since this isn’t very WordPress specific you’re probably better off asking this question in a PHP or JavaScript forum…

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘admin plugin’ is closed to new replies.