submit to the home page ...why?

amira - June 30, 2009 - 21:43

hi all,
i tried to develop my own module that select from data base and return result into a html tabel then support adding new value property that sure adding a row in gui and db , the problem is when i click save button(which save in the db) i hope to redirect to the same page because i created a hidden field that i wanna to check for it's value then according to this value i wanna insert in db with php code ...but i faced a problem which is redirect to the home page of drupal not the same page i'm in ...why?

this is the code

<?
function managelinks_block($op = 'list', $delta = 0, $edit = array())


if ($op == "list")
{
    // Generate listing of blocks from this module, for the admin/block page
    $block = array();
    $block[0]["info"] = t('Manage Link Block');
    return $block;
  }
 
else if ($op == 'view')
{
   $query = "SELECT title, url FROM managelinks";
   $queryResult =  db_query($query);

   $block_content = '<form id="f1" action="'.$_SERVER[PHP_SELF].'" method="POST">'; 
   $block_content .='<table id="t" border=3>';

  while ($links = db_fetch_object($queryResult))
  {
   $block_content .='<tr><td><input type="textbox" name="ti" value="'.$links->title.'" readonly="readonly"></td>';
   $block_content .='<td><input type="textbox" name="ur" value="'.$links->url.'" readonly="readonly"></td>';
   $block_content .='<td><input type="button" name="Edit" value="Edit" onclick= edit("'.$links->title.'") ></td>';
   $block_content .=   '<td><input type="button" name="delete" value="Delete" onclick=delete("'.$links->title.'") ></td>';
$block_content .='</br>';
$block_content .=   '</tr>';  
  }
 
  $block_content .='</table></form>';
  $block_content .='<td><input type="button" name="ADD" value="ADD" onclick=add("t") ></td>';


}



if ($block_content == '')
{   
    /* No content from a week ago.  If we return nothing, the block  
     * doesn't show, which is what we want.
     */
    return;
  }

  // set up the block 
  $block = array();
  $block['subject'] = 'Manage Your Links'; 
  $block['content'] = $block_content;
 
return $block;

} // end function
  ?>
 

<script language="javascript">
var count = "1";
function add(t)
{

                     var tbody = document.getElementById("t").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=\"ti\">";
                    td1.innerHTML = strHtml1.replace(/!count!/g,count);


// create table cell 2 ----- url
                     var td2 = document.createElement("TD")
                     var strHtml2 = "<INPUT TYPE=\"text\" NAME=\"ur\">";
                    td2.innerHTML = strHtml2.replace(/!count!/g,count);


                

var td3 = document.createElement("TD")
                    var strHtml3 = "<INPUT TYPE=\"submit\"  VALUE=\"Save\"   >";
                    td3.innerHTML = strHtml3.replace(/!count!/g,count);


var td4 = document.createElement("TD")
                    var strHtml4 = "<INPUT TYPE=\"hidden\"  name=\"old\" VALUE=\"insert\">";
                    td4.innerHTML = strHtml4.replace(/!count!/g,count);

                    // append data to row
                        row.appendChild(td1);
                        row.appendChild(td2);
                        row.appendChild(td3);
                        row.appendChild(td4);
                       
               // add to count variable
                          count = parseInt(count) + 1;
                     // append row to table
                           tbody.appendChild(row);
 
}

</script>

i believe your

siodong - July 1, 2009 - 02:29

i believe your $_SERVER[PHP_SELF] pointing to home page, as drupal generate a page from "q" parameter which hidden by clean url from htaccess and mod_rewrite. if you want to try create a form in drupal, you can start from here

refer page to itself in drupal

Lrrr - July 1, 2009 - 19:54

As siodong mentioned, PHP_SELF redirects to the root file index.php.

Here's a snippet that may help you.....

function build_form() {
global $base_url;
$path_self = "/" . drupal_lookup_path('alias', $_GET['q']);

$form .= "<div align=\"center\"><form name=\"makechange\" id=\"makechange\" method=\"post\" action=\"" . $base_url . $path_self . "\" />\r";

    $form .= "... FORM ELEMENTS HERE ....";

$form .= "</form></div>\r";
return $form;
}

thank u very much for helping

amira - July 4, 2009 - 10:12

thank u very much for helping me but i tried what u said and nothing changed....here is what i did:

<?
function managelinks_block($op = 'list', $delta = 0, $edit = array())

global $base_url;
if ($op == "list")
{
    // Generate listing of blocks from this module, for the admin/block page
    $block = array();
    $block[0]["info"] = t('Manage Link Block');
    return $block;
  }
 
else if ($op == 'view')
{

   $query = "SELECT title, url FROM managelinks";
   $queryResult =  db_query($query);
  
   $path_self = "/" . drupal_lookup_path('alias', $_GET['q']);
   $block_content = "<div align=\"center\"><form name=\"makechange\" id=\"f1\" method=\"get\" action=\"" . $base_url . $path_self . "\" />\r";
   $block_content .='<table id="t" border=3>';

  while ($links = db_fetch_object($queryResult))
  {
   $block_content .='<tr><td><input type="textbox" name="ti" value="'.$links->title.'" readonly="readonly"></td>';
   $block_content .='<td><input type="textbox" name="ur" value="'.$links->url.'" readonly="readonly"></td>';
   $block_content .='<td><input type="submit" name="Edit" value="Edit"></td>';
   $block_content .=   '<td><input type="submit" name="delete" value="Delete"> <INPUT TYPE="hidden"  name="delete" VALUE="delete"></td>';
$block_content .='</br>';
$block_content .=   '</tr>';  
  }
 
  $block_content .='</table></form></div>';
  $block_content .='<td><input type="button" name="ADD" value="ADD" onclick=add("t") ></td>';


}



if ($block_content == '')
{   
    /* No content from a week ago.  If we return nothing, the block  
     * doesn't show, which is what we want.
     */
    return;
  }

  // set up the block 
  $block = array();
  $block['subject'] = 'Manage Your Links'; 
  $block['content'] = $block_content;
 
return $block;

} // end function


  ?>
 

<script language="javascript">
var count = "1";
function add(t)
{

                     var tbody = document.getElementById("t").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=\"ti\">";
                    td1.innerHTML = strHtml1.replace(/!count!/g,count);


// create table cell 2 ----- url
                     var td2 = document.createElement("TD")
                     var strHtml2 = "<INPUT TYPE=\"text\" NAME=\"ur\">";
                    td2.innerHTML = strHtml2.replace(/!count!/g,count);


                

var td3 = document.createElement("TD")
                    var strHtml3 = "<INPUT TYPE=\"submit\"  VALUE=\"Save\"   ><INPUT TYPE=\"hidden\"  name=\"insert\" VALUE=\"insert\">";
                    td3.innerHTML = strHtml3.replace(/!count!/g,count);



                    // append data to row
                        row.appendChild(td1);
                        row.appendChild(td2);
                        row.appendChild(td3);
                 
                       
               // add to count variable
                          count = parseInt(count) + 1;
                     // append row to table
                           tbody.appendChild(row);
 
}


</script>

<?
if ($_GET[insert]=="insert")
{

  $query2 = "INSERT INTO managelinks (title ,url )VALUES ('".$_GET[ti]."', '".$_GET[ur]."')";
  $queryResult2 = db_query($query2);
  if($queryResult2)
  {
  drupal_set_message(t("added ok"));
  }
}

if ($_GET[delete]=="delete")
{

$query3 = "delete from managelinks where title='.$_GET[ti].'";


  $queryResult3 = db_query($query3);
  if($queryResult3)
  {
  drupal_set_message(t("Deleted ok"));
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.