wartotojas Posted February 25, 2010 Share Posted February 25, 2010 Hi everybody, I was implementing script from following tutorial: http://tutorialzine.com/2009/10/google-wave-history-slider-jquery/ and the thing is I want to insert additional data to mySQL when saving comment via ajax. I put all coding in my page and it works fine, but I want to add album ID together with comment. I retrieve album ID when I select album and use $alId = $_GET['alId']; It works as well for me, but when I click submit button for comment it is saved using code on file saveComment.php and sent there via ajax. How do I pass same variable $alId to saveComment.php to be inserted into database? Any suggestions and thoughts are appreciated. Link to comment https://forums.phpfreaks.com/topic/193381-how-can-i-pass-variable-to-separate-php-file/ Share on other sites More sharing options...
ialsoagree Posted February 25, 2010 Share Posted February 25, 2010 If your question is just how to pass the variable, you can do so either via POST or GET (as a POST request can contain both types of data, a GET request can only support GET data). To add the data via POST, you would have to modify line 70 of script.js: data: "comment="+encodeURIComponent(text)+"&parent="+parent, to... data: "comment="+encodeURIComponent(text)+"&parent="+parent+"&alID="+howeverAlIDIsSaved, Alternatively, you can pass it via get by modifying the URL the AJAX request is sent to at line 69: url: "ajax/saveComment.php", to... url: "ajax/saveComment.php?alID="+howeverAlIDIsSaved, Link to comment https://forums.phpfreaks.com/topic/193381-how-can-i-pass-variable-to-separate-php-file/#findComment-1018163 Share on other sites More sharing options...
wartotojas Posted February 26, 2010 Author Share Posted February 26, 2010 Sorry I'm pretty new to PHP and jQuery things, In my case alId is not static variable and it comes from database, so in other pages where navigating on website I used such expressions: <a href="?page=view-album&alId=<?php echo $al_id; ?>"> and it used to work, but I guess I cant insert them in script.js file as you suggested. Let's say I have $alId in main page, and I want it to be sent to saveComment.php somehow, and then to be inserted into my database. Here is full code of webpage I'm developing : <?php define("INCLUDE_CHECK",1); require 'connect.php'; require 'waveFunctions.php'; // Including the files for the DB connection and our custom functions $alId = $_GET['alId']; $comments_result = mysql_query("SELECT * FROM wave_comments WHERE al_id = '$alId' ORDER BY id ASC"); // Selecting all the comments ordered by id in ascending order $comments=array(); $js_history=''; while($row=mysql_fetch_assoc($comments_result)) { if($row['parent']==0) // If the comment is not a reply to a previous comment, put it into $comments directly $comments[$row['id']] = $row; else { if(!$comments[$row['parent']]) continue; $comments[$row['parent']]['replies'][] = $row; // If it is a reply, put it in the 'replies' property of its parent } $js_history.='addHistory({id:"'.$row['id'].'"});'.PHP_EOL; // Adds JS history for each comment } $js_history='<script type="text/javascript"> '.$js_history.' </script>'; // This is later put into the head and executed on page load //checks if session 'picLogin' exists session_name('picLogin'); session_set_cookie_params(2*7*24*60*60); session_start(); //If You are logging off if(isset($_GET['logoff'])) { //Destroy the session $_SESSION = array(); session_destroy(); //Redirect to welcome page 'index.php' header("Location: index.php"); exit; } require_once 'library/config.php'; require_once 'library/functions.php'; $albumPerPage = 10; $pageNumber = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1; $offset = ($pageNumber - 1) * $albumPerPage; $serial = $offset + 1; $al_user_name = $_SESSION['usr']; $sql = "SELECT al_id, al_user_name, al_name, al_date, al_image, DATE_FORMAT(al_date, '%Y-%m-%d') AS al_date, COUNT(im_album_id) AS al_numimage FROM tbl_album al LEFT JOIN tbl_image im ON al.al_id = im.im_album_id WHERE al.al_id= '$alId' GROUP by al_id ORDER BY al_name "; $result = mysql_query($sql . "LIMIT $offset, $albumPerPage") or die('Error, list album failed. ' . mysql_error()); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Google Wave-like History Slider </title> <link rel="stylesheet" type="text/css" href="waveDemo.css" /> <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script> <script type="text/javascript" src="waveScript.js"></script> <?php echo $js_history; ?> <!-- Outputting the addHistory functions --> </head> <body> <div id="main"> <?php if (mysql_num_rows($result) == 0) { ?> <table align="center" class="table_grey"> <tr> <td colspan="5">No album yet <?php $al_user_name ?></td> </tr> </table> <?php } else { $serial = $offset + 1; while ($row = mysql_fetch_assoc($result)) { extract($row); $al_numimage = "<a href=\"?page=list-image&album=$al_id\">$al_numimage</a>"; ?> <div class="container"> <table width="100%" class="clearfix"> <th align="center">Album Name</th> <th align="center"> Images</th> <th align="center"> </th> <tr> <td width="50%" align = "center"><a href="?page=view-album&alId=<?php echo $al_id; ?>"><img src="images/album/thumbs/<?php echo $row['al_image']; ?>" /><br /> </a><a href="?page=view-album&alId=<?php echo $al_id; ?>"><?php echo $al_name; ?></a></td> <td width="6%" align = "center"><?php echo $al_numimage; ?></td> <td width="42%" align ="left"><label class="grey" for="Author">Author: <?php echo $row['al_user_name']; ?> </label> <br /> <label class="grey" for="Date added">Date added: <?php echo $row['al_date']; ?></label> </td> </tr> <tr> <td align = "right"> </td> </tr> </table> <?php } // end while } ?> <table align="center" class="table_grey"> <tr> <td colspan="5" align="center"><?php $result = mysql_query($sql); $totalResults = mysql_num_rows($result); echo getPagingLink($totalResults, $pageNumber, $albumPerPage, "page=list-album"); ?> </td> </tr> </table> <div id="wave"> <div id="topBar">Comments</div> <div id="sliderContainer"> <div id="slider"></div> <div class="clear"></div> </div> <div id="commentArea"> <?php foreach($comments as $c) { showComment($c); // Showing each comment } ?> </div> <input type="button" class="waveButtonMain" value="Add a comment" onclick="addComment()" /> <div id="bottomBar"> </div> </div> </body> </html> Thus $alId should be sent to saveComment.php when "Add Comment" button is clicked. Link to comment https://forums.phpfreaks.com/topic/193381-how-can-i-pass-variable-to-separate-php-file/#findComment-1018438 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.