Jump to content

I can't figure out how to run multiple requests right.


HaLo2FrEeEk

Recommended Posts

Here's the situation, I have a page that let's users put in url's to images, then a page will get a random one out and display it everytime the url is refreshed.  It's a signature rotator.  I have it set up so the insertion process uses AJAX, as does the list of url's.  When a user puts in a new url, though, they still have to manually update the url list to show the new change.  I would like it to be one swift thing, the user pastes the url in the texbox, clicks insert, and the url is checked for validity, inserted (or not,) and the url list is updated to reflect the new url.  I tried putting the 2 funstions in the action part of the form I use to submit the url, so it runs the insert function, then the update function, but there are problems.  Both functions will run simultaneously, causing a few things to happen: if the url doesn't get inserted before the request to update the url list completes, then the url list doesn't get updated, since I have a loading function built in which basically displays a little "loading" box in the corner, telling the user that something is happening, both the insert and update command use the same function, which calls up the loading function, since there is no real on/off switch in the loading function (if it's on, turn it off; if it's off, turn it on, that's it), it calls the insert, turns on loading, calls update, turns off loading, then for some reason, one finishes and loading is turned back on and it stays on.  I need to be able to update the url list when a new url is inserted and I don't know how to get both functions to run, but one after the other (insert first, then update).  I also want the url list to be displayed when the page is loaded, currently, the user logs in, then has to click the "show url list" link to get it to display.  Anyone know how to help me out with this?  I can post snippets of code if need be, I know it can be difficult to visualize all this.

 

Thank you in advance.

Link to comment
Share on other sites

ok, why not put the two functions in the same php code that gets executed.  place them in the order you need them to fire off so that therecords will be written.

 

As for your urls being displayed when the page is loaded, you can do this in one of two ways.  Either use the onload of the body to fire off a javascript that calls an ajax function to populate the data, or

have your page be a .php, and build the page with a while loop to populate the fields and then display the output. 

 

I prefer the latter, just in case your visitors have javascript turned off, they will still have a dropdown to choose from.

Link to comment
Share on other sites

Well here, you guys can test it out here:

 

http://claninfectionist.com/misc/sigs/

 

username: test

password: test

 

The index page is a php page, but it has a header and footer and the header contains the body tag.  The header is used elsewhere on the site, too, so I can't put javascript in the onload, unless I put a switch in the index page of the sig rotator admin that tells the header to put the onload code in only for pages with that switch.  That might work, but it's a roundabout fix and I don't like those.

 

Here is the php code of my insert page:

 

<?php
session_start();
include('./functions.php');
if(!islogged()) {
  header("Location: login.php");
  }
require($_SERVER['DOCUMENT_ROOT'].'/config.php');
mysql_connect($host, $user, $pass) or die ($host_die.mysql_error());
mysql_select_db($dbname) or die($db_die.mysql_error());

$user_id = $_SESSION['user_id'];
$url = htmlentities($_POST['url']);
if(substr($url, 0, 7) != "http://") {
  die(message('This is not a valid URL:', 1));
  }
$ext = strtolower(substr($url, strrpos($url, '.')+1, strlen($url)));
if($ext !== "png" && $ext !== "gif" && $ext !== "jpg") {
  die(message('This is not a valid image, you must use either a png, gif, or jpg image.', 1));
  }

$query = "SELECT count(*) FROM `sig_rotator_data` WHERE `user_id` = ".$user_id." AND `sig_url` = '".$url."'";
if($count = mysql_query($query)) {
  $count = mysql_result($count, 0);
  if($count > 0) {
    die(message('That URL is already in the database under your name.', 1));
    }
  }

$query = "INSERT INTO `sig_rotator_data` (`user_id`, `sig_url`, `added`) VALUES ('".$user_id."', '".$url."', '".time()."')";
if(mysql_query($query)) {
  die(message('The URL was successfully added into the database.', 0));
  } else {
  die(message('Could not submit your URL, please try later...', 1));
  }
?>

 

The message() function just spells out the message in the first quotes, and the status is the second number (0 or 1, 0 being success and 1 being failure).  I use die so the script will immediately stop when that message is displayed and won't show anything else.  This is the javascript function I use to update the url list:

 

get('url_list.php', $user_id, '', '', '', 'url_list');

 

This is how it works:

 

get('

', [user id of the user logged in], '[img_id, of the url being edited]', '[action to take]', '[url being inserted]', '[div to update]')

 

Obviously these don't always all apply, so I use if statements to check if they exist before putting them into them into the post string being submitted.

 

So basically I don't know where to put that Javascript function inside the insert page to have it run after inserting the url.  It can only run with one of the possible messages that that page gives out, and that's the success one.  I don't need it to go when the url doesn't get put in, only when it does.

 

So, can you help me?

Link to comment
Share on other sites

hmm..  after seeing your code, i question whether AJAX needs to be used on anything except making sure the user has entered a valid url into the input box.  Until they do, I would disable the insert button.

 

Now to fix your problem, I would first determine whether the insert button was pressed ( IF ($_REQUEST["SUBMIT"])

 

Replace SUBMIT with whatever you named your INSERT button.

 

<?php
if ($_REQUEST["SUBMIT"]) {
  // put your current code to add to the database in here
  // I prefer to make functions out of alot of code lines, as it helps document what your trying to accomplish
  // and this helps after six months of not looking at the code to go in and fix with relative ease.
  // not to mention it prepares the code for possible OOP later.
  add_link_to_db();

}

build_select_statement(); 


?>

 

So... the way this works is that it will first check to see if any input came from the form, and if so add the information to the database.

Regardless of the whether input was sent, it will then rebuild the screen with the new dropdown.  Adding the new URL to the list if there was one.

Link to comment
Share on other sites

Well you're right, I don't need AJAX here, obviously, even for checking the url, but I want it because it saves the users time between loading the pages, and it's just convenient.  I'll name the submit button and try what you said and get back to you.

Link to comment
Share on other sites

why not just have one request send and return json so you dont need 2 requests as for using the onload on the body. you dont have to.

you can use this anywhere in your page so the function loads when the page is loaded

<script>
document.load=function(){
    //your function to load
}

</script>

Link to comment
Share on other sites

why not just have one request send and return json so you dont need 2 requests as for using the onload on the body. you dont have to.

you can use this anywhere in your page so the function loads when the page is loaded

<script>
window.onload = function(){
    //your function to load
}

</script>

Link to comment
Share on other sites

I will look into doing it that way.  I had considered adding that to the message that's returned when I insert a link, that way it would get printed to the status div.

 

I tried adding a onTimeOut so that it would run the update command in intervals, every 10 seconds or so, since it's only a small amount of info that I'm fetching, the overhead wouldn't be big.  I could not get the ontimeout to work though...at all.  I couldn't even get it to do an alert in the ontimeout, so something was going wrong.  I'll look at it further later tonight after work.

 

Thanks for the help, I'll be back if I need anything else.

Link to comment
Share on other sites

can I see some of the modified php code, with the functions add_link_to_db, and build_select_statement, or equivalant?

 

with the revised php code earlier, you could call ajax request to index.php in the main body onload, and again in the onsubmit for the form.

 

When called the first time, no form elements would be retrieved, so it would just show the dropdown.  When called fromt the form onsubmit, the form elements would be there, so it would update the database and then display your dropdown. 

 

Since all these were called from either onload or onsubmit, it would still have the ajax functionality you were wanting.

Link to comment
Share on other sites

I can't do it in the body onload because that's located in the header file which is not only used for this page, so I'd get errors on all my other pages, and I tried putting it in the onsubmit (well, the action attribute) of the form element, but it didn't work.  I suppose I could add an onsubmit attribute to the form, that way action would be the add link and the onsubmit would be the retrieve urls.  I can't post any php right now because I'm so tired I barely know what I'm typing (worked 7.5 hours today, but I got off a 10 and I work at 6 am tomorrow morning, til 5).  I'll get some stuff up tomorrow if I have time.

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.