Hello,

just making an update link using pop up box but dont know how would i be able to get the id though

here is my code

<?php 

                        $get_query = "SELECT * FROM shortenedurls WHERE uid=".$id;  
                        $query     = mysqli_query($connection, $get_query);

                        while($record = mysqli_fetch_array($query)) {
                            $url        = $record['url']; 
                            $longurl    = $record['longurl']; 
                            $ida        = $record['id']; 

                            echo "<div class='entries'>";
                            echo  "<div class='col-md'><a href='$longurl'>". $longurl   ."</a></div>";
                            echo  "<div class='col-md'><a href='$url'>". $url   ."</a></div>";
                            echo  "<div class='col-md smwidth1'>". $ida ."</div>";
                            echo  "<div class='col-sm smwidth2'><a href='#myModal2?id=$ida' data-toggle='modal' data-target='#myModal2'><i class='fa fa-pencil-square-o'></i></a></div>";
                            echo  "<div class='col-sm smwidth2'><a href='includes/delete.php?id=$id&delete=$ida&code=$coupen'><i class='fa fa-trash-o'></i></a></div>";
                            echo  "<div class='clear'></div>";
                            echo  "</div>";

                        }
                    ?>  

and to show on model

    <!-- Modal -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
       <form method="post" action="includes/update.php?id=<?php echo $id.'&code='.$coupen; ?>">
                <?php echo links($ida); ?>
                <input type="submit" value="update" name="update" class="logout" />
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
</div>
    </div>

links($ida) is a fuinction here is the coed for it

    function links($id) {
        global $connection;

        $get_query = "SELECT * FROM shortenedurls WHERE id='$id'";  
        $query     = mysqli_query($connection, $get_query);

        while($record = mysqli_fetch_assoc($query)) {
            $url1   = $record['url'];
            $url2   = $record['longurl'];
            $ida    = $record['id'];        

            $output  = "<div class='entries'>";
            $output .= "<div class='col-md'><input type='text' name='url' value='$url1' /></div>";
            $output .= "<div class='col-md'><input type='text' name='url2' value='$url2' /></div>";
            $output .= "<div class='col-md'><input type='text' name='short' value='$ida' /></div>";
            $output .= "<div class='clear'></div>";
            $output .= "</div>";

            return $output;
        }   
    }

Well, if you submit a form, the data doesn't just become available in regular variables (like $id), it becomes available in a special var. Depending on your form's method (get or post), it becomes available in $_GET or $_POST.

Seeing that your form's method is post, your data would be available under the $_POST array, e.g. $_POST['id']. You could try if that works?

Also a notice here: using the global indicator in a function is generally considered to be bad practice, because you never know which files may alter the variable you're importing from the global scope. Rather you'd pass the variable to the function, so that you're always certain that you have the right data available. For example, your function would then become function links($id, $connection). Even better, you could create a class that groups all database functions and data. See this link for the first tutorial I could find on using classes in PHP.

Yes I know about the post method but I am asking is how can i get id from this part as this is javasrcipt

echo "<div class='entries'>";
                                echo  "<div class='col-md'><a href='$longurl'>". $longurl   ."</a></div>";
                                echo  "<div class='col-md'><a href='$url'>". $url   ."</a></div>";
                                echo  "<div class='col-md smwidth1'>". $ida ."</div>";
                                echo  "<div class='col-sm smwidth2'><a href='' data-toggle='modal' data-target='#myModal2' ><i class='fa fa-pencil-square-o'></i></a></div>";
                                echo  "<div class='col-sm smwidth2'><a href='includes/delete.php?id=$id&delete=$ida&code=$coupen'><i class='fa fa-trash-o'></i></a></div>";
                                echo  "<div class='clear'></div>";
                                echo  "</div>";

If i had to get the id i mean a user wants to update a record we would need the id of it like i could have placed a link like this

<a href='coupon.php?id=$id&code=$coupon' data-toggle='modal' data-target='#myModal2' >

right but now it could'nt be done as there is a mixcture of pop up box though

I am a bit confused. Which ID is it that you want to use? An HTML ID? To pass to a script that handles your AJAX call?

Maybe it would help if you would highlight the lines your question is about?

Okay sure let me explain you .Like let suppose there are 5 listings which are in php loop pulled up from database from that 5 results i would like to edit them or any one of them so what i will do i will took an id which was auto generated so I wil obviously use that id to took it up and thorough which the list or the record could be updated

<?php 
                        $get_query = "SELECT * FROM shortenedurls WHERE uid=".$id;  
                        $query     = mysqli_query($connection, $get_query);
                        while($record = mysqli_fetch_array($query)) {
                            $url        = $record['url']; 
                            $longurl    = $record['longurl']; 
                            $ida        = $record['id']; 
                            echo "<div class='entries'>";
                            echo  "<div class='col-md'><a href='$longurl'>". $longurl   ."</a></div>";
                            echo  "<div class='col-md'><a href='$url'>". $url   ."</a></div>";
                            echo  "<div class='col-md smwidth1'>". $ida ."</div>";
                            echo  "<div class='col-sm smwidth2'><a href='#myModal2?id=$ida' data-toggle='modal' data-target='#myModal2'><i class='fa fa-pencil-square-o'></i></a></div>";
                            echo  "<div class='col-sm smwidth2'><a href='includes/delete.php?id=$id&delete=$ida&code=$coupen'><i class='fa fa-trash-o'></i></a></div>";
                            echo  "<div class='clear'></div>";
                            echo  "</div>";
                        }
                    ?>  

in the above code you can see this line echo "<div class='col-sm smwidth2'><a href='#myModal2?id=$ida' data-toggle='modal' data-target='#myModal2'><i class='fa fa-pencil-square-o'></i></a></div>"; where it clearly defines that this is the link to be edited but i cannot place any href link as its for the pop up box

and here is the pop up box

    <!-- Modal -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
       <form method="post" action="includes/update.php?id=<?php echo $id.'&code='.$coupen; ?>">
                <?php echo links($ida); ?>
                <input type="submit" value="update" name="update" class="logout" />
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
</div>
    </div>

here now you can see the id is required in order to pull up that record which the user want to edit it and update it make sense??

Thank You

Ahhh, so what you want to achieve is that when the user clicks the link, a popup (modal) opens up with a form that can be used to edit the record that was clicked?

If so, I think you should either look in the library that you use to open that modal (if you use one) how you can pass on data to that modal window. Or, if you are using some kind of custom implementation, you will probably need to write some Javascript to set the ID of the clicked element and then use more Javascript to pass it on to the modal window that is opened.

If you need help with that last part, let me know, and let's then also see what you can write/have written yourself :).

Member Avatar for diafol

I believe the $id in the modal should be $ida?

Anyway, when you open the modal, have an event handler that transfers the 'id' value from the button (or link) to the url specified in form action. I'm assuming you're using bootstrap:

var coupen = <?=$coupen?>; //I have no idea what this is or from where

$('#myModal2').on('show.bs.modal', function (event) {
  var el = $(event.relatedTarget);
  var id = el.data('id');
  var modal = $(this)
  modal.find('form').attr('action', 'includes/update.php?id=' + id + '&code=' + coupen);
  //you could do an ajax to get the data for the record and populate the form here
  //else take data from the html table and populate the form fields
});

This should work if you change the PHP to:

 echo  "<div class='col-sm smwidth2'><a href='#myModal2' data-toggle='modal' data-id='$ida' data-target='#myModal2'><i class='fa fa-pencil-square-o'></i></a></div>";

The code above was modified from the Bootstrap page: http://getbootstrap.com/javascript/#modals where you could find the info yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.