Jump to content

channeling mult links to a single Solved with thanks in final post


niche

Recommended Posts

I modified the script at: http://www.w3schools...hp_ajax_php.asp to the script found at: http://www.lincolnsr...est_120506.html It only works with names that start with a "B". This requires a separate script for each link in the div called link_container. That will be a lot of scripts someday. I can't think of a way to channel all the links to a single script that sends the user to the same place they'd go as if they just clicked on the original link. Can you think of a way? If so, what's your thinking?

Edited by niche
Link to comment
Share on other sites

If the link has an href attribute, just send all the links to one function that gets the href property from the link.

Link to comment
Share on other sites

I'm currently using that to define the script that's connected to the link. For example the link "Bob" currently has href attribute of "bob.php". What's the thinking that will connect the "Bob" link to the href attribute "direct_all_links_from_here.php" so when "Bob is clicked the user gets sent to the "Bob" page from "direct_all_links_from_here.php"? I need it to work similarly for the "Barbara" link, same way for the "Barney" link and the "Beatrice" link. I need to avoid having a separate script for each firstname. I also want to avoid using a dropdown box to populate an input field.

Edited by niche
Link to comment
Share on other sites

Can't you store content for bob page in database, use ref for 'bob' to look through database for name ref, and drag all content for 'bob' and place in content returned from ajax response into ONE single page.

Edited by dsonesuk
Link to comment
Share on other sites

You can have a query string on the link so that PHP knows which page to go to:

<a href="direct_all_links_from_here.php?page=Bob">Bob</a><a href="direct_all_links_from_here.php?page=Barbara">Barbara</a><a href="direct_all_links_from_here.php?page=Beatrice">Beatrice</a>

  • Like 1
Link to comment
Share on other sites

I'm following you dsonesuk. I have Bob's data stored in a table. How do I get the value for "Bob" from Bob's link so I can use it in a SQL query when the link sends me to a utility script called "direct_all_links_from_here.php"?

Edited by niche
Link to comment
Share on other sites

Ingolme, can I get access to the value in the query string at the beginning of "direct_all_links_from_here.php"? If so how?

Edited by niche
Link to comment
Share on other sites

Ah, the GET array! Count me as a first time user. Thanks Ingolme. Dsonesuk, please tell me a little about what you meant by "content returned from ajax response into ONE single page"?

Link to comment
Share on other sites

you have a link

<a href="mypage.php?page=Bob">Bob</a>

you have database table with field names 'name' and 'content' and values would be similar to belowname | bob |content |<p>Eu fugiat nulla pariatur. Ullamco laboris nisi ut labore et dolore magna aliqua. Sed do eiusmod tempor incididunt cupidatat non proident, lorem ipsum dolor sit amet. Duis aute irure dolor excepteur sint occaecat mollit anim id est laborum. Qui officia deserunt ut enim ad minim veniam, cupidatat non proident.</p><p>In reprehenderit in voluptate ullamco laboris nisi ut aliquip ex ea commodo consequat. Consectetur adipisicing elit, lorem ipsum dolor sit amet. In reprehenderit in voluptate sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Velit esse cillum dolore quis nostrud exercitation duis aute irure dolor. Sunt in culpa eu fugiat nulla pariatur. Mollit anim id est laborum. Consectetur adipisicing elit, cupidatat non proident, in reprehenderit in voluptate.</p><p>Ut labore et dolore magna aliqua. Sunt in culpa excepteur sint occaecat lorem ipsum dolor sit amet. Velit esse cillum dolore.</p><p>In reprehenderit in voluptate eu fugiat nulla pariatur. Duis aute irure dolor velit esse cillum dolore ut aliquip ex ea commodo consequat. Ut labore et dolore magna aliqua. Ut enim ad minim veniam, in reprehenderit in voluptate lorem ipsum dolor sit amet. Duis aute irure dolor mollit anim id est laborum.</p>|within in mypage.php page you would have

$current_person=$_GET['page'];$result = mysql_query("SELECT * FROM mytable WHERE name ='$current_person'");if (!$result) {die("Query to show fields from table failed");}while ($row = mysql_fetch_assoc($result)){echo '<h2>'.$row['name'].'</h2>';echo '<div id="content">'.$row['content'].'</div>';}

whatever persons name is sent with querystring 'page=Bob' with be pick up, is searched for in database table and whatever content related to that specific name ref will be shown in the while loops$row['content'].You may want to use a id ref ?page=22, as you can have many people with first name 'bob'.This same method can be used in ajax, where the above processed content is added to the main document page in a specific id container 'main_content' when recieved back from mypage.php

  • Like 1
Link to comment
Share on other sites

Didn't know you could display content that way. How is that ajax?

Link to comment
Share on other sites

In ajax instead of<a href="mypage.php?page=Bob">Bob</a>which will cause the page to reload, to retrieve the content for 'bob' from DB. Ajax uses basically the same system to retrieve content using php through a querystring xmlhttp.open("GET","mypage.php?q="+str,true); where str would equal 'bob' the difference is ajax only retrieves and returns specific data from the database to go in the main_content container, where normally the whole page is returned with <html><head><body> etc when the page reloads.

<a href="#" onclick="ajaxfunction('bob');">Bob</a>

which would run the ajax script send the string 'bob' to a php page, that will also search and retrieve content for 'bob' and echo out the result, this result is then return to ajax script, where you direct it to where it should be placed for example a div element with id ref 'main_content'

  if (xmlhttp.readyState==4 && xmlhttp.status==200)     {     document.getElementById("main_content").innerHTML=xmlhttp.responseText;     }   }

this will cause instant change without the page requiring to reload.

  • Like 1
Link to comment
Share on other sites

I'd keep the href attribute in case Javascript is disabled, then return false to prevent the link from opening a new page.

  • Like 1
Link to comment
Share on other sites

Thanks a lot dsonesuk. I'm following you. In this situation, that would mean an ajax call within an ajax call. Very intriguing. I'll pursue this in another topic after I work-up some code. Thanks to ingolme and dsonesuk for their help on this topic.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...