gw32 Posted August 5, 2011 Share Posted August 5, 2011 This is probably a very easy question for experts like you guys, but it's driving me crazy. I have a URL passed variable that is used in the first section of my code. I use that to generate a second variable. The first variable gets the name of a file from a MySQL table. It also gets a directory from the table. Here is my dilemma, I need to pass the $name and $path to separate sections on the same page. For some reason I can't seem to get them down that far. Here is what I have for the first section of code. <?php $id=$_POST['cat']; //Passed from URL directory ID from table $name=$_POST['subcat']; //Passed from URL image name from table // Select Directory name to be used for $path $sql = "SELECT directory FROM directory WHERE DID = '$id'"; $result = mysql_query($sql) or die('A error occured: ' . mysql_error()); while ($row = mysql_fetch_assoc($result)) { $path = $row['directory']; // Preview image just selected print "<img class='thumbNormal' src='http://mysite.com/images/$path/$name' width=200 onclick='SelectImg($id)'>"; // Verify that the path select is correct print "<br>"; print $path."/".$name; } // Attempt to pass these variables to rest of page $path = $_GET['directory']; $name=$_GET['subcat']; ?> By the way I have different groups of things to do seperated by <tr>'s and <td>'s in the group that I need these variable passed to I have this. $upload="http://mysite.com/images/$path"; echo $upload; //we will give an unique name, for example the time in unix time format $image_name=$name; //the new name will be containing the full path where will be stored (images folder) $newname=$upload . "/" . $image_name; echo "<br>"; echo $newname; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; echo $copied; $errors=1; }}}} in my echo $upload I get "http://mysite.com/images/" - no path in my echo $newname I get "http://mysite.com/images//" - no image name What am I doing incorrectly? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/243937-passing-variables-to-self/ Share on other sites More sharing options...
@ Posted August 5, 2011 Share Posted August 5, 2011 are you using $_GET or $_POST to fetch the values for the variables: seems like you are trying to use both at the same time?!? $id=$_POST['cat']; //Passed from URL directory ID from table $name=$_POST['subcat']; //Passed from URL image name from table $path = $_GET['directory']; $name=$_GET['subcat']; Quote Link to comment https://forums.phpfreaks.com/topic/243937-passing-variables-to-self/#findComment-1252557 Share on other sites More sharing options...
gw32 Posted August 5, 2011 Author Share Posted August 5, 2011 I thought that is what the manual said to do. First use $_POST to retrieve variables from url and then use $_GET to process them to the rest of the application. So which is correct and how do I pass the variables to the rest of the page? Quote Link to comment https://forums.phpfreaks.com/topic/243937-passing-variables-to-self/#findComment-1252579 Share on other sites More sharing options...
trq Posted August 5, 2011 Share Posted August 5, 2011 $_GET contains values passed through the url. $_POST contains values posted to your script via a form using the post method. how do I pass the variables to the rest of the page? I'm not sure what you mean. Your data is available anywhere on the page within either $_GET or $_POST. Quote Link to comment https://forums.phpfreaks.com/topic/243937-passing-variables-to-self/#findComment-1252585 Share on other sites More sharing options...
AyKay47 Posted August 5, 2011 Share Posted August 5, 2011 1. if you are planning on using the $path variable correctly, you will include the code in your while loop.. 2. I assume that your form has a method of POST.. 3. keep $name=$_POST['subcat']; as is, this variable will be available to your entire script..$_GET array is only used when values are passed through the URL or a form method of GET Edit: Thorpe i'm tired of being your echo.. Quote Link to comment https://forums.phpfreaks.com/topic/243937-passing-variables-to-self/#findComment-1252586 Share on other sites More sharing options...
gw32 Posted August 5, 2011 Author Share Posted August 5, 2011 OK, the complete code for what I am trying to do. 1) select directory and image name. 2) preview that image 3) select and preview new image 4) upload new image with old image name. (This part is necessary, as I do not want to edit multiple XML files generated for a Carousel flash app) Complete code: Common stuff <?php ?> <html> <head> <style> body { background-color:#000000; color:#E27907; font-family:Verdana,Arial; font-size:10pt; letter-spacing:2; } .thumbNormal { border:4px solid #000000; } .thumbSelected { border:4px solid #ff0000; } .prevImage { border: 8px solid #ccc; width: 200px; height: 100px; } </style> <script language=javascript> var lastID = 0; function SelectImg(id) { if (lastID > 0) { document.getElementById(lastID).className = "thumbNormal"; } document.getElementById(id).className = "thumbSelected"; document.getElementById(0).src = document.getElementById(id).src; lastID = id; } function LoadTrigger() { SelectImg(1); } window.onload = LoadTrigger; </script> </head> Selecting Current Image <body> <table border=0 width="1000"> <tr> <td valign='top' width='45%' height="200">Select Directory and Image Name<p> <?php include("dd.php"); ?> <td valign='top' width='5%' height="200"> </td> <td valign='top' width='55%' height="200">Current Image to Replace.<p> <?php $id=$_POST['cat']; $name=$_POST['subcat']; $sql = "SELECT directory FROM directory WHERE DID = '$id'"; $result = mysql_query($sql) or die('A error occured: ' . mysql_error()); while ($row = mysql_fetch_assoc($result)) { $path = $row['directory']; print "<img class='thumbNormal' src='http://mysite.com/images/$path/$name' width=200 onclick='SelectImg($id)'>"; print "<br>"; print $path."/".$name; } ?> <td width=15> </td> <td valign=top> </td> </tr> <tr> <td valign=top></td> </tr> Select and Preview New Image <td valign='top' width='45%' height="200">Preview New Image.<p> <script type="text/javascript"> function setImage(file) { if(document.all) document.getElementById('prevImage').src = file.value; else document.getElementById('prevImage').src = file.files.item(0).getAsDataURL(); if(document.getElementById('prevImage').src.length > 0) document.getElementById('prevImage').style.display = 'block'; } </script> <form> <input type="file" name="myImage" onchange="setImage(this);" /><p> </form> <img id="prevImage" style="display:none;" /> </td> Upload New Image and Rename to Old Image Name <td valign='top' width='5%' height="200"> </td> <td valign='top' width='45%' height="200">Upload New Image.<p> <?php //define a maxim size for the uploaded images in Kb define ("MAX_SIZE","100"); //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This variable is used as a flag. The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an errro occures. //If the error occures the file will not be uploaded. $errors=0; //checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Unknown extension!</h1>'; $errors=1; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } $upload="http://mysite.com/images/" . $path; echo $upload; //we will give an unique name, for example the time in unix time format $image_name=$name; //the new name will be containing the full path where will be stored (images folder) $newname=$upload . "/" . $image_name; echo "<br>"; echo $newname; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; echo $copied; $errors=1; }}}} //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { echo "<h1>File Uploaded Successfully! Try again!</h1>"; } ?> <!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" --> <form name="newad" method="post" enctype="multipart/form-data" action=""> <table> <tr><td><input type="file" name="image"></td></tr> <tr><td><input name="Submit" type="submit" value="Upload image"></td></tr> </table> </form> End Code </table> </body> </html> Now if you follow it through as 1 single file, What am I doing wrong to pass the $path and $name to the bottom section of the file? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/243937-passing-variables-to-self/#findComment-1252667 Share on other sites More sharing options...
AyKay47 Posted August 5, 2011 Share Posted August 5, 2011 1. if you are planning on using the $path variable correctly, you will include the code in your while loop.. 2. I assume that your form has a method of POST.. 3. keep $name=$_POST['subcat']; as is, this variable will be available to your entire script..$_GET array is only used when values are passed through the URL or a form method of GET Quote Link to comment https://forums.phpfreaks.com/topic/243937-passing-variables-to-self/#findComment-1252673 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.