Jump to content

[SOLVED] Noob - problem with Sessions (possibly)


colinsp

Recommended Posts

I have an input form (form1) that is working properly. Part way through the flow of this form I want to allow the user to upload a photo if they want to. The way I have this working at the moment is I have a link which says insert image. If clicked this opens a new window with form 2 on it to allow the user to select and upload the image to the images directory (this all works fine). I then set the session variable with the name of the image uploaded. The user closes the window and reverts to form 1.

 

The problem that I have is on form 1, I have echoed the value of the session variable to the form to ultimately put the image name into a MySQL database BUT because the session was already started on form 1 the previous image filename is shown rather than the one just uploaded. Is there a way to refresh reading of the session variable partway through a form?

 

I am sure there must be an easier way to do this but my PHP skills are not yet sufficient to allow me to incorporate the two forms into one with two different buttons (one for the form and one for the image). I tried for a couple of days and failed miserably.

 

Any thoughts or links to tutorials etc most welcome.

 

Colin

I have an input form (form1) that is working properly. Part way through the flow of this form I want to allow the user to upload a photo if they want to. The way I have this working at the moment is I have a link which says insert image. If clicked this opens a new window with form 2 on it to allow the user to select and upload the image to the images directory (this all works fine). I then set the session variable with the name of the image uploaded. The user closes the window and reverts to form 1.

 

The problem that I have is on form 1, I have echoed the value of the session variable to the form to ultimately put the image name into a MySQL database BUT because the session was already started on form 1 the previous image filename is shown rather than the one just uploaded. Is there a way to refresh reading of the session variable partway through a form?

 

I am sure there must be an easier way to do this but my PHP skills are not yet sufficient to allow me to incorporate the two forms into one with two different buttons (one for the form and one for the image). I tried for a couple of days and failed miserably.

 

Any thoughts or links to tutorials etc most welcome.

 

Colin

 

Sure there is...

Look the first thing i would ask is how do you communicate between the two forms... when a user is done with form2 (the image) where do you post it to...

is it the same php file that process these two?

what i am thinking is that you have the old values because the old HTML that was generated before form2 was submitted is showing.

 

there are a few ways of doing what you need, you can either use JavaScript(AJAX), or PHP to get the SESSION variable.

 

It would really help if i could see the code in question.

 

 

When the user is done with form 2 it is closed after the image has been uploaded and the session variable set.

 

This is the tail end of the form 2 code.

 

<?php
if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
              $imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "images/".$imagename;
              move_uploaded_file($source, $target);

              $imagepath = $imagename;
              $save = "images/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file

              list($width, $height) = getimagesize($file) ; 

              $modwidth = 150; 

              $diff = $width / $modwidth;

              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

              imagejpeg($tn, $save, 100) ; 

              $save = "images/sml_" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file

              list($width, $height) = getimagesize($file) ; 

              $modwidth = 80; 

              $diff = $width / $modwidth;

              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

              imagejpeg($tn, $save, 100) ; 
            	echo "Thumbnail: <img src='images/sml_".$imagepath."'>"; 
			$_SESSION['image'] = "images/sml_".$imagepath;
				//echo $_SESSION['image'];
          }
        }
?>

 

Then this is the form section of form 1

 

<form name="new_advert" action="" method="POST">
<table align="center" width="550" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td width="45%">Item: <input name="item" type="text" autocomplete="off" size="25" maxlength="25" /></td>
  <td width="55%">Category:
    <select name="category">
	<?php
		$sql = "SELECT id,category FROM type";

		$result = mysql_query($sql);

		while($row = mysql_fetch_array($result))
			{
  				echo "<option value=\"".$row['id']."\">".$row['category']."\n  ";
			}
		mysql_free_result($result);
		$updated = "";
	?>
</select></td>
  </tr>
  <tr>
    <td colspan="2">Description: 
      <textarea name="description" cols="70" rows="3" ><?php echo "$description";?></textarea></td>
    </tr>
    <!--Insert image code here-->
    <tr>
    <td align="center"> <a href="#"
          	onclick="window.open('upload_image.php','new','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=550, height=250')">Add image</a>
      
    </td>
    <td align="center">
    <?php session_start();
$img_loc = $_SESSION['image'];
echo $img_loc;
 ?>
    </td>
    </tr>
    <!--end insert image-->
    <tr>
    <td>Price: <input name="price" type="text" autocomplete="off" size="10" maxlength="10" />Currency: <select name="currency" size="1">
      <option value="1">£</option>
      <option value="2">$</option>
      <option value="3" selected>€</option>
        </select></td>
        
    <td>Location: <input name="location" type="text" autocomplete="off" size="25" maxlength="25" /></td>
  </tr>
  
  <tr>
    <td>Name: <input name="name" type="text" autocomplete="off" size="30" maxlength="30" /></td>
    
    <td>Email: 
      <input name="email" type="text" autocomplete="off" size="40" maxlength="40" /></td>
  </tr>
  <tr><td height="10"></td>
  </tr>
  <tr><td colspan="2" align="center">
  <input type="submit" name="Submit" id="Submit" value="Submit" >
  
  </td></tr>
</table>
</form>

 

Does that help?

Oh yes

ok basically you restart the session everytime form1 is loaded

 

you have to check if the session exist before any output to the page and then only if it doe not you should use session_start();

 

as soon as u use it it will start a new one

and then we all know that in a new session u will probably have session id but nothing more

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.