Hyperlink from last field in script Multiple Select Dropdown List with AJAX

Place for comments, problems, questions, or any issue related to the JavaScript / PHP scripts from this site.
maneerat
Posts: 4

Hyperlink from last field in script Multiple Select Dropdown List with AJAX

hi, I use the code from https://coursesweb.net/ajax/multiple-sel ... ist-ajax_t to create my database in my web.
In the example file, it has 5 field in a table, I insert a new field in this table (field name : file) and I want to make hyperlink in this field but I can not set this field to link. How can I do?

Admin
Hi
What data is in the field "file"? If the table has 5 columns, one column must be the "id". In the $ar_cols variable set an array with the names of the other fields, for example:

Code: Select all

$ar_cols = array('site', 'menu', 'categ', 'links', 'file');  
- Then, in your web page, after the last dropdown select list ("links") will be displayed the data added in the last field ("file").
- The data is displayed as it is saved in that field.

If the data in the last field is an URL address and you want to be displayed as a link into an <a> tag, change this line of code in the "ajax_select.js" file:

Code: Select all

document.getElementById(preid+col).innerHTML = request.responseText;
With this code:

Code: Select all

// "file" is the name of the last field in the mysql table
if(col =='file') document.getElementById(preid+col).innerHTML = '<a href="'+ request.responseText +'">'+ request.responseText +'</a>';
else document.getElementById(preid+col).innerHTML = request.responseText;
- Or, another way to display a hyperlink from data added in the last field, change this line of code (in select_list.php):

Code: Select all

else echo $re_html;
With this one:

Code: Select all

else echo ($col == $ar_cols[$last_key]) ? '<a href="'. $re_html .'">'. $re_html .'</a>' : $re_html; 

maneerat
data is in the field "file" is an document file, example: manualweb.pdf, .doc, .ppt, and other

Code: Select all

$ar_cols = array('site', 'menu', 'categ', 'links' );  // 5 cols this okay
then, how can I do display hyperlink from data 'links' to the field 'file'. I mean I want to put a file in database and user can download when click link.

Admin
I think it's better to put /save the file on server (into a folder), then, in the "links" field to put the path (url) to that file. And make hyperlink, with <a> tag like it is explained in the previous answer.

- If you save the file content into a mysql table field, it's more complicated. You have to create a separated php file with a script that selects a row from that table, according to data from url, and outputs the content saved in the "file" column (with a header() according to content type), using a select like this:

Code: Select all

$link = addslashes(strip_tags($_GET['link']));
$sql = "SELECT file FROM table_name WHERE links='$link' LIMIT 1";
// here the rest of your code to get and output data from "file", With a header() according to the type of that content 
For example, if you make that select into a php file named "getfile.php", make a hyperlink in the multiple select lists script to open the "getfile.php" with data from "links" passed in url.

Code: Select all

else echo ($col == $ar_cols[$last_key]) ? '<a href="getfile.php?link='. $re_html .'">'. $re_html .'</a>' : $re_html; 

maneerat
sorry until now, I don't understand it well. Please kindly give me more information about syntax code by return. Now, I already created "getfile.php", how should I do next?

Admin
You should know how to make a connection and selection to a mysql database in php. Also, you need to know the type of the content that is accessed and to add the header() function according to that type. You can search on the net for: "php header content type".
It is your business to make the code that selects and outputs the file. Anyway, you can post the code you have created and details about what changes you made in the "multiple select list script", and what it is not working in your script.

maneerat
I change this line of code in the "ajax_select.js" file:

Code: Select all

// "file" is the name of the last field in the mysql table
    if(col =='file') document.getElementById(preid+col).innerHTML = '<a href="'+ request.responseText +'">'+ request.responseText +'</a>';
    else document.getElementById(preid+col).innerHTML = request.responseText;
and change this line of code in the "select_list.php" file:

Code: Select all

else echo ($col == $ar_cols[$last_key]) ? '<a href="file/getfile.php?link='. $re_html .'">'. $re_html .'</a>' : $re_html;
and create "getfile.php" in folder file

Code: Select all

$link = addslashes(strip_tags($_GET['links']));
$sql = "SELECT file FROM site WHERE links='$link' LIMIT 1";
// here the rest of your code to get and output data from "file", With a header() according to the type of that content

header("Content-Disposition: attachment; filename=".$_GET["id"].""); 
readfile($_GET["id"]); 
display
test.jpg
You do not have the required permissions to view the files attached to this post.

Admin
1. If you made changes in the "select_list.php" file, you not need to modify the "ajax_select.js" file; let the JS file as it is.

2. You don't know how to work with mysql in php. You have to make a connection to database before applying a SQL query.
This is a website with courses and tutorials to learn web programming and site building. So, it is a good place where you can learn php and mysql. For example, from this page: PHP PDO - Select query, fetch you can learn how to make a selection to mysql database with PHP PDO.

3. I not understand your code in the "getfile.php" file. Way you use $_GET["id"]? Do you have "id=something" in the URL that accesses the "getfile.php"?
You said that the file content is stored in the mysql table, in the "file" column, then, way you use: readfile($_GET["id"]);? This instruction reads a file from server, not from database.

- My opinion is that you still need to study some lessons and tutorials about php-mysql and files on server, because php coding is not copy-paste, you need to know the logic of the code, and what is the result of the instructions you use.

Similar Topics