I'm trying to get an input value passed via jquery and setting into a php variable, but I don't know how to do that.
This button have the value that I want to send:
<button type='button' data-a='".$fila['idPlaza']."' href='#editarUsuario' class='modalEditarUsuario btn btn-warning btn-xs' data-toggle='modal' data-backdrop='static' data-keyboard='false' title='Editar usuario'><img src='../images/edit.png' width='20px' height='20px'></button>
Then I have this js code to send the value:
$(document).on("click", ".modalEditarUsuario", function (e) {
e.preventDefault();
var self = $(this);
$("#a").val(self.data('a'));
$(self.attr('href')).modal('show');
});
Finally I have this bootstrap modal
<!-- MODAL EDITAR-->
<div id="editarUsuario" class="modal fade modal" role="dialog">
<div class="vertical-alignment-helper">
<div class="modal-dialog vertical-align-center">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h2 class="modal-title">Editar usuario</h2>
</div>
<form name="formularioModificaUsuario" method='post' action="usuarios.php">
<div class="modal-body">
<input type="text" class="form-control" name="idPlaza" id="a">
<?php
$valueA = ???
?>
</div>
<div class="modal-footer">
<button type="reset" id="cancelar" class="btn btn-danger" data-dismiss="modal" value="reset"><img src='../images/cancel.png' width='20px' height='20px'> Cancelar</button>
<button type="submit" name="submitModifica" id="submit" class="btn btn-primary" value="submit"><img src='../images/save_changes.png' width='20px' height='20px'> Guardar cambios</button>
</div>
</form>
</div>
</div>
</div>
</div>
My question is, how could I get the value of id='a' input into php variable $valueA to use after?
<button type='button' data-a="<?echo $fila['idPlaza'];?>" href='#editarUsuario' class='modalEditarUsuario btn btn-warning btn-xs' data-toggle='modal' data-backdrop='static' data-keyboard='false' title='Editar usuario'>
<img src='../images/edit.png' width='20px' height='20px'>
</button>
Put below code in footer before end of </body> tag.
<!-- MODAL EDITAR-->
<div id="editarUsuario" class="modal fade modal" role="dialog">
<div class="vertical-alignment-helper">
<div class="modal-dialog vertical-align-center">
<div class="modal-content">
</div>
</div>
</div>
</div>
Use Script like this.
<script>
$('.modalEditarUsuario').click(function(){
var ID=$(this).attr('data-a');
$.ajax({url:"NewPage.php?ID="+ID,cache:false,success:function(result){
$(".modal-content").html(result);
}});
});
</script>
Create NewPage.php, and paste the below code.
(Remember, this page name NewPage.php is used in script tag also. if you are planning to change the name of this page, change page name there in script tag too. Both are related.)
<?
$ID=$_GET['ID'];
?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h2 class="modal-title">Editar usuario</h2>
</div>
<form name="formularioModificaUsuario" method='post' action="usuarios.php">
<div class="modal-body">
<input type="text" class="form-control" name="idPlaza" id="a">
<?php
$valueA = $ID;
?>
</div>
<div class="modal-footer">
<button type="reset" id="cancelar" class="btn btn-danger" data-dismiss="modal" value="reset">
<img src='../images/cancel.png' width='20px' height='20px'> Cancelar</button>
<button type="submit" name="submitModifica" id="submit" class="btn btn-primary" value="submit">
<img src='../images/save_changes.png' width='20px' height='20px'> Guardar cambios</button>
</div>
</form>
Php loads before anything on your page, so you cannot set a php variable using jQuery or javascript, but the opposite can be done.
If you have the value of the text field initially when loading then you can set it via php on load, otherwise you will need to submit your form to use the value of the input in php.
The following code is valid, php setting javascript
<?php
$valueA = "I am value A";
?>
<script language="javascript">
var jsValueA = "<?php echo $valueA; ?>";
alert(jsValueA); // alerts I am value A
</script>
The following code is invalid, javascript setting php
<input type="text" class="form-control" name="idPlaza" id="a">
<?php
$valueA = $("#a").val();
?>
This is wrong because when PHP executed the input field would not have been created yet, besides jQuery cannot be executed within PHP scope. javascript and jQuery are used at browser level to manage user interaction and DOM objects and events while PHP is used at server level and not browser.
So if you want to set a php variable, you can only set it when the server script loads, i.e php gets executed, which comes before anything else in the page. Or you can grab a value from a posted form $_POST or URI query string $_GET
Related
Im trying to fix a strange error with my code with the Bootstrap 4 Modal code. I've simply just copied and pasted the code from here: https://getbootstrap.com/docs/4.0/components/modal/#events
This is for an automotive website and Im trying to pass stock and VIN via the data attribute as requested. The question is how this works with a foreach loop. It works on a single Wordpress page or post but won't pull the data from each section of the loop.
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-stock="<?php echo $vehicle['stock_number'];?>">Check Availability</button>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Stock</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Recipient:</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Message:</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
And my Jquery code
(function($) {
$('#exampleModal').on('modal.fade.show', function (event) {
alert( "clicked" );
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('stock') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use $ here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-title').text('New message to ' + recipient)
modal.find('.modal-body input').val(recipient)
})})( jQuery );```
Functionality wise this works, just not within the dynamic pages that are being generated in the foreach loop. The buttons have the proper data-attribute applied but the data doesn't seem to pass to the modal from the loop. I have a development page up where you can take a live look here, the button in question is the "Check Availability" http://windsor.sixomedia.ca/inventory/New/
Try this
var recipient = button.data('data-stock');
Hope that helps.
i have modal popup in loop with database values. In popup there is a reply textarea and i want to post that value using ajax. But i am getting empty value of textarea when i alert value on click event using jquery. Here is my code
CODE:
<?php
$sn=1;$rep=1;
foreach($view_queries as $q) {?>
Reply
?>
<!-- create reply model -->
<div id="create_reply<?php echo $rep;?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<form id="send_reply" method="post" antype="multipart/form-data">
<input type="hidden" name="query_id" value="<?php echo $q->query_id;?>" />
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Reply To Query</h4>
</div>
<div class="modal-body">
<label>Query:</label>
<p><?php echo ucfirst($q->query);?></p>
<div class="form-group">
<label>Reply:</label>
<textarea rows="3" name="mesg" id="<?php echo $sn;?>mesg" class="form-control"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" id="" data-target="<?php echo $sn;?>mesg" class="btn btn-default form_click">Reply</button>
</div>
</form>
</div>
</div>
</div>
<!-- create reply model ends -->
<?php $sn++; $rep++; }?>
HERE IS THE SCRIPT FOR CHECKING VALUE IN ALERT:
<script>
$(document).ready(function(){
$(document).on("click",".form_click",function(event){
event.preventDefault();
var a=$('#' + $(this).data('target')).text();
alert(a);
});
});
</script>
You should use .val() instead of .text(). text() get's the HTML contents of a HTML element. val() is used for getting contents of HTML inputs.
Also as Smit Raval already stated, id's should be unique.
You could give the fields an id with and index set in PHP.
So in every loop you increment the index and add that to your id values, like so:
id="<?=$index?>_mesg".
And in your button do:
<button type="button" data-target="<?=$index?>_mesg" id="" class="btn btn-default form_click">Reply</button>
And then in your jquery you can do var a=$('#' + $(this).data('target')).val();
See the added data-target in your button. And the $(this).data('target') in the jquery for retrieving the value of the target data attribute.
<script>
$(document).ready(function(){
$(document).on("click",".form_click",function(event){
event.preventDefault();
var a= $(this).parents("form.send_reply").find("textarea.mesg").text();
alert(a);
});
});
</script>
change id="mesg" to class="mesg" in your loop.Also make sure you don't repeat the same ID in your loop. Change id="send_reply" to class="send_reply"
I have modal popup in loop with database values. The modal box contains a form which have hidden input value and a textarea to insert message. I am able to get the value of textbox with the help of stackoverflow.
Reference :
bootstrap modal textarea showing empty alert on click jquery
But when i alert the hidden value in alert box it does not change according to the loop in alert box. The value of hidden element is changing in loop when i check using inspect element but when i alert it, it is showing same value.
CODE:
<?php
$sn=1;$rep=1;
foreach($view_queries as $q) {?>
Reply
?>
<!-- create reply model -->
<div id="create_reply<?php echo $rep;?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<form id="send_reply" method="post" antype="multipart/form-data">
<input type="hidden" name="query_id" value="<?php echo $q->query_id;?>" />
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Reply To Query</h4>
</div>
<div class="modal-body">
<label>Query:</label>
<p><?php echo ucfirst($q->query);?></p>
<div class="form-group">
<label>Reply:</label>
<textarea rows="3" name="mesg" id="<?php echo $sn;?>mesg" class="form-control"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" id="" data-target="<?php echo $sn;?>mesg" class="btn btn-default form_click">Reply</button>
</div>
</form>
</div>
</div>
</div>
<!-- create reply model ends -->
<?php $sn++; $rep++; }?>
HERE IS THE SCRIPT FOR CHECKING VALUE IN ALERT:
<script>
$(document).ready(function(){
$(document).on("click",".form_click",function(event){
event.preventDefault();
var a=$('#' + $(this).data('target')).text();
var query_id=$('#query_id').val();
alert(query_id);
});
});
</script>
Easier option is use data-* attribute to persist query id as you are using data-target
<button type="button" data-id="<?php echo $q->query_id;?>" data-target="<?php echo $sn;?>mesg" class="btn btn-default form_click">Reply</button>
Then you can easily use
var query_id= $(this).data('id');
Problem with your implementation is that You have not specified ID of hidden field, thus must be getting undefined while fetching value.
<input type="hidden" name="query_id" value="<?php echo $q->query_id;?>" />
To get its value use DOM relationship of current element.
Use .closest(selector) to traverse up to common parent then use .find() to get the element using Attribute Equals Selector [name=”value”]
Relevant code
var query_id= $(this).closest('form') //target common parent
.find('[name=query_id]') //target element
.val();
$(document).ready(function() {
$(document).on("click", ".form_click", function(event) {
event.preventDefault();
var query_id = $(this).closest('form') //target common parent
.find('[name=query_id]') //target element
.val();
console.log(query_id);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="send_reply" method="post" antype="multipart/form-data">
<input type="hidden" name="query_id" value="1" />
<div class="modal-footer">
<button type="button" id="" class="btn btn-default form_click">Reply</button>
</div>
</form>
I am trying to disable a button if input is not valid. I tried to use PHP but I think its not the right approach. Any suggestions guys? The code is here below:
<form action="" method="" enctype="multipart/form-data" data-toggle="validator">
<div class="modal fade" id="qtyModal" tabindex="-1" role="dialog" aria-labelledby="qtyModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="qtyModal">Quantity</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="form-group col-md-6">
<label for="quantity<?=$i;?>">Quantity:</label>
<input type="number" name="quanitity" id="quantity" value="<?= $quantity ;?>" min="0" class="form-control" data-minlength="1" pattern="([0-9]|[1-8][0-9]|9[0-9]|100)" data-error="Please enter a Positive Number between 0 - 100!!!">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<?php if(($quantity == '') || ($quantity >= 0 )) :?>
<button type="button" class="btn btn-primary" onclick="updateQtyFunction();jQuery('#qtyModal').modal('toggle');return false;">Save changes</button>
<?php endif;?>
</div>
</div>
</div>
</div>
</form>
This button (shown below) I am trying to disable (taken from the code above):
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<?php if(($quantity == '') || ($quantity >= 0 )) :?>
<button type="button" class="btn btn-primary" onclick="updateQtyFunction();jQuery('#qtyModal').modal('toggle');return false;">Save changes</button>
<?php endif;?>
</div>
Below also find the jQuery Function:
function updateQtyFunction(){
var qtyNumber='';
if(jQuery('#quantity')!=''){
qtyNumber = jQuery('#quantity').val();
}
jQuery('#qtyPreview').val(qtyNumber);
}
This is very easy to do with Javascript and you don't really need jQuery for it.
Just give the button you want to disable an id and in your function set the disabled attribute of the button to true.
<!DOCTYPE html>
<html lang="nl">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="modal-footer">
<!-- Add an id to the button -->
<button type="button" id="button_1" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="updateQtyFunction();">Save changes</button>
</div>
<script>
function updateQtyFunction()
{
var qtyNumber='';
if (jQuery('#quantity') != '')
{
qtyNumber = jQuery('#quantity').val();
$('#button_1').attr('disabled', 'true'); // Disable the button (jQuery)
document.getElementById('button_1').disabled = 'true'; // Disable with Javascript
}
jQuery('#qtyPreview').val(qtyNumber);
}
</script>
</body>
</html>
For PHP, something like this could work:
if(isset($_POST['button']))
echo '<button type="button" name="button" />';
If the form action is "post" and an input tag with name of "button" has a value. Then this button will show up and not if otherwise.
If you need to disable the button based on a condition that can change without reloading the page, and you are using bootstrap (which it seems you are) you can add the class .disabled to the button with jQuery. Then you can remove it to enable it when the condition is met.
On another side if the page requires to be reloaded for the button to be enabled, instead of adding / removing the class with jQuery, you can do it in PHP.
I have a button and I am trying to pass value to a model.Inside button I have name="className" value="CPS210-CompSci-I (4)" button is type submit.
<button type="submit" data-toggle="modal" data-target="#myModal"
class="btn btn-info btn-md" name="className" value="CPS210-CompSci-I (4)">
<span class="glyphicon glyphicon-comment"></span> Comment
</button>
I have a model. Inside model I have php $className =$POST['className'];
echo "$className";
<div ng-app class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><?php echo "Note for $firstname $lastname" ?></h4>
</div>
<div class="modal-body">
<div class="form-group">
<textarea class="form-control" ng-model="note"
placeholder="Here you can write a a note for the student. " id="message"
required data-validation-required-message="Please enter a Goal"></textarea>
<p class="help-block text-danger"></p>
<label class="light">
</div>
<hr>
<?php $className = $_POST['className'];
echo "$className";
?>
<h4>Note Regarding:{{note}} </h4>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Variables are only added to the $_POST array after a form has been submitted. You're page must "refresh" before the array is filled. When the modal triggers and opens a new modal box on the scree, this is not considered a page refresh, therefore the value of the button you clicked is not added to the array.
You either need to put your button inside a form tag, which will trigger the modal to open when the page "refreshes" and the value in that key exists. This would be annoying and defeat the whole purpose of bootstrap modals.
You're other, more likely option, is to use javascript to move the value in the button to the spot in the modal when the button is clicked.
$('button[name="className"]').click(function(){
var className = $(this).val();
$('#class-name').html(className);
});
And edit your modal where you want the class name to this:
<hr>
<p id="class-name"></p>
<h4>Note Regarding:{{note}} </h4>