1

This might look a bit strange because I haven't slept for 3 days now but i promised to finish the scripts today while i am kind of a exhausted beginner huh. I have a loop showing pictures, below every picture is a link. When you click it, the popup with add comment form shows up. But actually I have problems to save the comments to database because I don't know how to transfer variables. I will show you this in a simple pseudo-code:

    while(lets_say_50){
      $x = picture;
      echo $x;
      echo "<a href>links opening popup with comment form</a>
           <div id='popup'>
           <form>
           <textarea> some comment </textarea>
           <input type='submit' method='post'>
           </form>
           </div>"
    }

    if(isset($_POST['submit'])){
     //here i want to get $x value from above, but it must be exactly the one from the actual loop turn.
     //whatever i do, it always gives me the $x from the last picture...
    }
5
  • Add <input type="hidden" name="ineedthisvalue" value="" /> to your form and change it's value on $x when opening form. Commented Dec 24, 2014 at 3:14
  • 1
    I think i will get some sleep now because i dont understand anything guys XD Thank you for the answers and see you tomorrow! Commented Dec 24, 2014 at 3:18
  • do you have some javascript code, that is showing the form? how do you open it? Commented Dec 24, 2014 at 3:30
  • @vladkras yea there is a popup jquery thing that i took from internet Commented Dec 24, 2014 at 12:18
  • That ought not to be verbatim, as you’d be outputting multiple elements with the same ID. Is that in your real code, or is that just an error in the question? Commented Dec 24, 2014 at 19:55

3 Answers 3

1

This is because you're overwriting the value of $x.

This is what is happening:

$x = Firstpicture;
$x = Secondpicture:
$x = Lastpicture3;

$x Will always equal the last value assigned to it.

Sign up to request clarification or add additional context in comments.

Comments

0

You’ll have to thread the value of $x through. First, you’ll have to make sure that the link provides that value to its destination. For example,

<a href="post_comment.php?comment_id=5">reply</a>

Then that page can use the comment_id GET parameter to add a hidden field to the form:

<input type="hidden" name="comment_id" value="5">

(Alternatively, if you’re posting to the same page (and it looks like you are), you can omit the hidden field and pass the comment ID in the query string again.)

Then, when you finally have $_POST['submit'] set, you should be able to retrieve the comment_id you’ve threaded through these interactions.

4 Comments

suppose form must appear on clicking image without reloading page
@vladkras: Then there’s already some JavaScript in place and that would need to be modified. No JavaScript was in the question, though, and the javascript tag was not applied, so until I know otherwise, I’m going with my current assumption.
up to you, but we both know he is doing it with some JS code, he not guessed to post yet, cause he don't see now how it could help
I tried to pass it through input but not it sends the first one all the time. I dont understand it, my brain is too small xD And yes, i am doing everything at the same page
0

Just put a uniq variable like a picture id into the href

 while(lets_say_50){
    $x = picture;
    echo $x;
    echo "<a href='link/#IMAGE_ID'>links opening popup with comment form</a>"
 }

Then in the popup have some javascript to read the hash and put it into a hidden variable in the form

<form method="post" action="post_comment.php">
    <textarea> some comment </textarea>
    <input type='submit' >
    <input name="imageid" id="image_id" name="image_id" type="hidden" value=""/>
</form>

<script>
   document.getElementById('image_id').value=document.location.hash;
</script>

1 Comment

this looks great but i will still have to sent it from the js script to the php :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.