Jump to content

Replace input type submit with a href


DanielHardy

Recommended Posts

Hi All, I am dynamically producing output of submit buttons like this:

 

echo '<div id="downloads" style="font-family:Arial;width:900px;height:30px;float:left;margin-bottom:5px;background-color:#069e0d;line-height:30px;color:#ffffff;"><div id="filename" style="float:left;padding-left:20px;width:600px;height:30px;border-right:5px solid #ffffff;">'.$name.'</div><div id="date" style="text-align:center;font-size:11px;float:left;width:125px;border-right:5px solid #ffffff;height:30px;">'.$date.'</div><div id="date" style="float:left;width:140px;text-align:center;font-size:11px;height:30px;"><input type="submit" name="admin[]" value="'.$id.'"  style="height:30px;width:145px;border:none;background-image:url(download.png);color:transparent;background-color:#069e0d;"></div></div>'."\n";  
}

 

I need to somehow reproduce this, but use a href instead of the button.

 

The "admin" value is used by the following piece of code:

 


<?php  
   
mysql_connect("*******", "*******", "*****") or die(mysql_error());
mysql_select_db("***********") or die(mysql_error());

$sql = "SELECT id,files,name,date,status,user FROM user1_files ORDER by id DESC";  
$cookie = $_COOKIE['ID_my_site'];
   $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); 


   while(list($id,$files,$name,$date,$status,$user)=mysql_fetch_row($result)){

    $updated = FALSE;  
    if(count($_POST) > 0){  
        $admin = $_POST['admin'];  
        array_map('intval',$admin);  
        $admin = implode(',',$admin);  
       
        mysql_query("UPDATE user1_files SET status=1 WHERE id IN ($admin)") or trigger_error(mysql_error(),E_USER_ERROR);  

       $updated=TRUE; 
   header("Location: $files"); 
   }  
   }
   ?>

 

 

 

All that does is update a value from 0 to 1 when the button has been clicked.

 

I want the same functionality but it does not seem to work when I replace the button with an image like follows:

 


echo '<div id="downloads" style="font-family:Arial;width:900px;height:30px;float:left;margin-bottom:5px;background-color:#069e0d;line-height:30px;color:#ffffff;"><div id="filename" style="float:left;padding-left:20px;width:600px;height:30px;border-right:5px solid #ffffff;">'.$name.'</div><div id="date" style="text-align:center;font-size:11px;float:left;width:125px;border-right:5px solid #ffffff;height:30px;">'.$date.'</div><div id="date" style="float:left;width:140px;text-align:center;font-size:11px;height:30px;"><a href="' .$files . '" name="admin[]" value="'.$id.'"  style="height:30px;width:145px;border:none;background-image:url(download.png);color:transparent;background-color:#069e0d;"></a></div></div>'."\n";  
}

 

Hope that all makes sense

 

Thanks in advance

 

Dan

Link to comment
Share on other sites

the <a href>'s should work fine, but you need to add the variables to the url string.

imagine you link points to 'page2.php', you will have to ad whatever variables and values you want, so instead of something like this

<a href="page2.php">bla bla bla</a>

you'll have:

<a href="page2.php?var1=hello&var2=world">bla bla bla</a>

then, to grab the variables, instead of $_POST, use $_GET

 

(Mind you, sending variables like this through the url string, when they will affect your database values is quite insecure and can lead to problems. It would be fine if on the next page you were just SELECT(ing) stuff from the database, but you're also changing it with an UPDATE statement.)

Link to comment
Share on other sites

Thanks for your reply.

 

I'm not sure that will help me. The redirect redirects to a database based URL.

 

Let me explain.

 

if($status==1){ 
      
echo '<div id="downloads" style="font-family:Arial;width:900px;height:30px;float:left;margin-bottom:5px;background-color:#cccccc;line-height:30px;"><div id="filename" style="float:left;padding-left:20px;width:600px;height:30px;border-right:5px solid #ffffff;">'.$name.'</div><div id="date" style="text-align:center;font-size:11px;float:left;width:125px;border-right:5px solid #ffffff;height:30px;">'.$date.'</div><div id="date" style="float:left;width:140px;text-align:center;font-size:11px;height:30px;"><a class="downloads" href="' .  $files . '" title="Your File"><img src="viewed.png"></a></div></div>'."\n";  
}

 

The above code (for each) displays the name, date and URL of all database table objects where status = 1. This all displays fine and the "$files" variable does indeed link to where it should.

 

However, When status equals 0, I am using the following code:

 

$sql = "SELECT id,files,name,date,status,user FROM user1_files ORDER by id DESC";  
$cookie = $_COOKIE['ID_my_site'];
   $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); 

if($status==0){
   while(list($id,$files,$name,$date,$status,$user)=mysql_fetch_row($result)){



    $updated = FALSE;   
    if(count($_POST) > 0){  
        $admin = $_POST['admin'];  
        array_map('intval',$admin);  
        $admin = implode(',',$admin);  
       
        mysql_query("UPDATE user1_files SET status=1 WHERE id IN ($admin)") or trigger_error(mysql_error(),E_USER_ERROR);  

       $updated=TRUE; 
   echo "<meta http-equiv='refresh' content='1;URL=". $files ." ' />";
   }  
   }
   }

 

to update the status to 1 when the button is clicked. However, the files variable for all objects is automatically redirecting to the id with the lowest id. I.e I want to link to the url associated with 6, it ill link to URL associated with 1.

 

Thanks again

 

Dan

Link to comment
Share on other sites

SOLVED,

 

Was simply a case of specifying to the update function only to carry out the actions when the id is equal to one in the database.

 

I.e

 

Instead of

$sql = "SELECT id,files,name,date,status,user FROM user1_files ORDER by id DESC";  
$cookie = $_COOKIE['ID_my_site'];
   $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); 

if($status==0){
   while(list($id,$files,$name,$date,$status,$user)=mysql_fetch_row($result)){



    $updated = FALSE;   
    if(count($_POST) > 0){  
        $admin = $_POST['admin'];  
        array_map('intval',$admin);  
        $admin = implode(',',$admin);  
       
        mysql_query("UPDATE user1_files SET status=1 WHERE id IN ($admin)") or trigger_error(mysql_error(),E_USER_ERROR);  

       $updated=TRUE; 
   echo "<meta http-equiv='refresh' content='1;URL=". $files ." ' />";
   }  
   }
   }

 

I used


if(count($_POST) > 0){  
        $admin = $_POST['admin'];  
        array_map('intval',$admin);  
        $admin = implode(',',$admin);

$sql = "SELECT id,files,name,date,status,user FROM user1_files WHERE id IN ($admin)";  
$cookie = $_COOKIE['ID_my_site'];
   $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); 
if($status==0){
   while(list($id,$files,$name,$date,$status,$user)=mysql_fetch_row($result)){



    $updated = FALSE;   
      
       
        mysql_query("UPDATE user1_files SET status=1 WHERE id IN ($admin)") or trigger_error(mysql_error(),E_USER_ERROR);  

       $updated=TRUE; 
   echo "<meta http-equiv='refresh' content='1;URL=". $files ." ' />";
   }  
   }
   }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.