Update using modal and php

I am doing a CRUD operation using only php and bootstrap. I am stuck in updating now because I cannot send $_GET['updateid'] to my modal.

These are my href, delete href works, but update does not work.

 <a href="#myModal?updateid=<?php echo $row['sched_id'];?>" data-toggle="modal" class="btn btn-warning" data-target="#myModal">Update</a> <a href="home.php?deleteid=<?php echo $row['sched_id'];?>" class="btn btn-danger">Delete</a> 

This php should take updateid from href and pass it to updateStud () function

 if(isset($_GET['updateid'])){ $mupdate_id = $_GET['updateid']; if(isset($_POST['modal-submit'])){ $msubj = $_POST['modalsubject']; $msect = $_POST['modalsection']; $mday = implode("", $_POST['modalday']); $mstrTime = $_POST['modalstarttime']; $mendTime = $_POST['modalendtime']; $auth_user->updateSchedule($msubj,$msect,$mday,$mstrTime,$mendTime,$mupdate_id); $schedRow = $auth_user->readSchedule(); } } 

this is my function update that takes all the data and executes the request

 public function updateSchedule($msubj,$msect,$mday,$mstrTime,$mendTime,$mupdate_id){ $stmt = $this->conn->prepare("UPDATE `schedule` SET `subject_db` = :msubj, `section_db` = :msect, `sched_day` = :mday, `start_time` = :mstrTime, `end_time` = :mendTime WHERE `sched_id` = :mupdate_id "); $stmt->bindparam(":msubj", $msubj); $stmt->bindparam(":msect", $msect); $stmt->bindparam(":mday", $mday); $stmt->bindparam(":mstrTime", $mstrTime); $stmt->bindparam(":mendTime", $mendTime); $stmt->bindparam(":mupdate_id", $mupdate_id); $stmt->execute(); return $stmt; } 
+5
source share
4 answers

You need to use this script for your modal.

 <a data-toggle="modal" data-target="#modal-target" data-id="<?= $idyouwant ?>">Change</a> 

You can fill in your modal html that you want with ajax / jquery like this javascript

 $('#modal-target').on('show.bs.modal', function(ev){ var modal = $(this); var link = $(ev.relatedTarget); var id = link.data('id'); modal.find('.modal-body').load('yourserver/?param=' + id); // will load modal body that you want } 

then you can customize your script the way you want.

+1
source

If you only need to update the data with modal and display the response inside the modal code, you do not need to add #myModal to the link. You should use it like this:

 "?updateid=<?php echo $row['sched_id'];?>" 

instead

 "#myModal?updateid=<?php echo $row['sched_id'];?>" 

Hope this helps.

0
source

If you use the same modal code with different activating links on your page, you need to fill in through javascript / jquery. The modal does not accept parameters from the target line unless you change the boostrap.js function directly (I do not recommend messing with js vendor files). You can make a built-in onclick method if for some reason you cannot add your own script to the page.

JQuery

<a href="#myModal" data-toggle="modal" class="btn btn-warning" data-target="#myModal" onclick="$('#updateid').val('<?= $row['sched_id'] =?>')">Update</a>

You will need hidden input in your form <input type='hidden' id='updateid' name='updateid'/> , which will then pass the parameter when submitting the form using the GET method.

If you use this as the only link to your modal, you can select your field in the modal <input type='hidden' name='updateid' value='<?= $row['sched_id']?>'/>

0
source

@tjfo made a comment in the comments on your original question and right. The browser does not call your script because of the "#Modal? ..." link. When you tell the browser to load a new "#" place on the same page, the browser does not contact the server to get a new copy of the page.

If you want to do this work without using javascript to send messages to the server, you can create unique access points, each of which has its own URL on the server. This will force the browser to reload the page for each call, and it will send a request to the server to complete the action.

Similarly, we could change the settings of the web server so that all new URLs are redirected to your main script (index.php, for example).

None of these approaches is recommended because your script uses $ _POST links and the browser does not send POST data when loading a new page.

The purest method is to use the bootstrap button to call the javascript function with the required identifier. Use javascript to create the url. Package ALL your data in POST data (see note after paragraph). Use javascript to call the server (read in AJAX) with your POST data, and then process the response accordingly.

(Note: it makes no sense to send GET and POST data on the same request --- note that if you insist that you can send an identifier from time to time using GET parameters, you can use $ _REQUEST to access given $ _POST and $ _GET at the same time).

I am happy to work this out with you in the comments section. Submit more of your code, and more people can provide more help.

-1
source

Source: https://habr.com/ru/post/1263543/


All Articles