How to identify which button is pressed and send that to server? - php

In the while loop I have doctor name, specialist and payment from my database. Each doctor has an <input type="button"> named "did". I want that when I press a button to contact a specific doctor, the value of $row3['did'] will sent to my contact.php page. But I cannot understand where to put it in the <input type="button"> so that I can identify which button is pressed and send its value to contact.php.
while($row3 = mysql_fetch_array($result3))
{
?>
<div class="col_1_of_4 span_1_of_4">
<div class="title-img">
<div class="title"><img src="images/Crystal_Clear_user.gif" alt=""></div>
<!--<div class="title-desc"><p>FACILITY 1</p></div>-->
<div class="clear"></div>
</div>
<h4 class=head>Doctor name: <?php echo $row3['dName'];?></h4>
<p>Specialist: <?php echo $row3['specialist'];?></p>
<p>Payment: <?php echo $row3['payment'];?></p>
<hr>
<div id='contact-form'>
more
<!--<button id="findHelp" class="btn btn-primary"><a href="?did=<?php// echo $row3['did'];?>" >Find help</a></button>-->
<!--<a href="data/contact.php?did=<?php //echo $row3['did'];?>" ></a>-->
<!--<input type='button' name='contact' id='contact' value="Message" class='contact demo btn btn-primary'/>-->
<form method='post' action='contact.php'>
<input type='button' name='did' id='did' value="Message" class='contact demo btn btn-primary'/>
</form>
</div>
<!-- preload the images -->
<div style='display:none'>
<img src='images/loading.gif' alt='' />
</div>
</div>
<?php
}
?>

There is no need to add the form..simply send the id with link when button is clicked and get that id on contact .php page..
OR use as following
<script src="jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$('.contact').click(function(){
var this_rel=$(this).attr('rel');
location.href="contact.php?id="+this_rel;
});
});
</script>
<input type='button' name='did' id='did' value="Message" class='contact demo btn btn-primary' rel="<?php echo $row3['did'];?>"/>

try if(isset($_POST['button_name']) && $_POST['button_name'] === 'button_value')

Use a hidden value inside the form, and assign the doctor details there
<form method="post" action="contact.php">
<input type="hidden" name="thedoctor" value="<?php echo $row3['did'];?>"/>
<input type="button" name="did" id="did" value="Message" class="contact demo btn btn-primary"/>
</form>
And then find the selected button by $button_clicked = $_POST['thedoctor']

This will be your PHP code
<?php
while($row3 = mysql_fetch_array($result3))
{
?>
<div class="col_1_of_4 span_1_of_4">
<div class="title-img">
<div class="title"><img src="images/Crystal_Clear_user.gif" alt=""></div>
<!--<div class="title-desc"><p>FACILITY 1</p></div>-->
<div class="clear"></div>
</div>
<h4 class=head>Doctor name: <?php echo $row3['dName'];?></h4>
<p>Specialist: <?php echo $row3['specialist'];?></p>
<p>Payment: <?php echo $row3['payment'];?></p>
<hr>
<div id='contact-form'>
more
<input type='button' name='did' id='did' value="Message" class='contact demo btn btn-primary' rel="<?php echo $row3['did'];?>"/>
</div>
<!-- preload the images -->
<div style='display:none'>
<img src='images/loading.gif' alt='' />
</div>
</div>
<?php
}
?>
and above that put some jquery code
<script src="jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$('.contact').click(function(){
var this_rel=$(this).attr('rel');
location.href="contact.php?id="+this_rel;
});
});
</script>
Dont forget to write query of database, because that was not in your example, thats why i did not mention that. Also add jquery.js to its appropriate place so jquery code can run. Please do this trick and let me know..

Just use buttons :
HTML
<form method="POST">
<button type="submit" name="btn_action_1" value="abc">Message</button>
<button type="submit" name="btn_action_1" value="def">Message</button>
<button type="submit" name="btn_action_1" value="ghi">Message</button>
<button type="submit" name="btn_action_1" value="jkl">Message</button>
<button type="submit" name="btn_action_2" value="123">Message</button>
<button type="submit" name="btn_action_3" value="456">Message</button>
</form>
PHP
<?php
if(isset($_POST['btn_action_1'])) {
echo 'btn_action_1 was pressed with value: '. $_POST['btn_action_1'];
}
if(isset($_POST['btn_action_2'])) {
echo 'btn_action_2 was pressed with value: '. $_POST['btn_action_2'];
}

Related

html form doesn't work after Jquery load()

I have a problem with jQuery load() and html forms.
After loading a div from a php. file into a div, the form doesn't work anymore:
jquery:
<script>
$(document).ready(function (){
$("#buttest").click(function (){
$("#ajax").load("Vokabeltrainer_sec.php?Lektion=<?php echo $lection ?> #area");
});
});
</script>
HTML-Form:
<?php
echo '<form action="Ergebnis.php" method="post">';
generateRadio($translation, $firstWord, $i, $index, $lection);
echo '<form>';
?>
The .php function is generating radio buttons.
Before reloading the div evrything works fine! Of course there is also a submit-botton:
<button type="submit" name="submit" value="Submit" id="subbut" class="btn btn-default">weiter</button>
Sorry for my bad english!
EDIT:
<div id="ajax">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Übersetzung auswählen</h3>
</div>
<div class="panel-body">
<?php
echo '<form action="Ergebnis.php" method="post">';
generateRadio($translation, $firstWord, $i, $index, $lection);
echo '<form>';
?>
</div>
</div>
<br>
<button type="submit" name="submit" value="Submit" id="subbut" class="btn btn-default">weiter</button>
<div id="test">
<button type="button" id="buttest">TEST</button>
</div>
</div>
</div>
The div with id="ajax" should be reloaded whith nearly the same div but with a different php function.

How to get input hidden value from multiple forms?

I am having unlimited multiple dynamic forms containing only single input hidden field having submit button and trying to get the value of that form whose button is clicked. I don't know why this is not happening.
I've read these How to get the Specific Form Field value using JQuery and Submit a specific form with JQuery Ajax but still no result.
controller
public function order()
{
$data['fruits'] = $this->catering_model->getrecess('Recess Fruits','Category Recess');
$this->load->view('ChoiceLaunch/order',$data);
}
order.php
<?php $i=0;
if(!empty($fruits)){
?>
<div class="col-sm-12"><h3 align="center"><?php echo $fruits[0]->catsubname; ?></h3><br>
<!-- while($res_item=mysqli_fetch_array($cat_item_sql)){ -->
<?php $i=0;
foreach($fruits as $lrd1){
$i++;
?>
<form method="post" action="">
<div class="col-sm-4 col-md-3">
<div class="thumbnail">
<img style="height: 177px;width: 200px;" src="<?php echo 'http://localhost:8080/catering/uploads/'.$lrd1->picturename;?>" alt="...">
<div class="caption protitle">
<p style="height: 12px;width: 200px;margin-top: 9px;margin-bottom:29px;"><?php echo $lrd1->itemname; ?></p><br>
<input type="hidden" name="quantity" id="quantity<?php echo $i;?>" value="<?php echo $lrd1->itemname; ?>" size="2">
<button type="submit" id="send" class="btn btn-info">Add To Order</button><br>
<?php echo '$'.$lrd1->price; ?>
</div>
</div>
</div>
<?php
}}
?>
</form>
</div>
<script>
$('#send').click(function(){
//var selection= $(this).attr('id');
// $( "[id='quantity']" ) .each(function(){
// alert($(this).val());
// }); //running bt showing all at once
$( "form:input[type=hidden]" ) .each(function(){
alert($(this).val());
})
});
</script>
I am using CodeIgniter.
1st: Id must be unique so use class instead
<button type="submit" class="btn btn-info send">Add To Order</button><br>
and in js
<script>
$('form').on('submit',function(e){
e.preventDefault();
var hiddenVal = $(this).find('input[type="hidden"]').val();
alert(hiddenVal);
});
</script>
and in php I think you closed the </form> in a wrong place
<?php $i=0;
foreach($fruits as $lrd1){
$i++;
?>
<form method="post" action="">
<div class="col-sm-4 col-md-3">
<div class="thumbnail">
<img style="height: 177px;width: 200px;" src="<?php echo 'http://localhost:8080/catering/uploads/'.$lrd1->picturename;?>" alt="...">
<div class="caption protitle">
<p style="height: 12px;width: 200px;margin-top: 9px;margin-bottom:29px;"><?php echo $lrd1->itemname; ?></p><br>
<input type="hidden" name="quantity" id="quantity<?php echo $i;?>" value="<?php echo $lrd1->itemname; ?>" size="2">
<button type="submit" class="btn btn-info send">Add To Order</button><br>
<?php echo '$'.$lrd1->price; ?>
</div>
</div>
</div>
</form>
<?php
}
?>
if you have more then one hidden field you need to use .each()
<script>
$('form').on('submit',function(e){
e.preventDefault();
$(this).find('input[type="hidden"]').each(function(){
alert($(this).val());
});
});
</script>
Your id never be quantity because you included a number in the final of each quantity word
Change de hidden input to this:
<input type="hidden" name="quantity" id="quantity<?php echo $i;?>" value="<?php echo $lrd1->itemname; ?>" class="quantity" size="2">
And change the each jquery to this:
$( ".quantity" ).each(function(){
alert($(this).val());
}); //running bt showing all at once
I would personally use a data-* attribute instead of hidden elements. This will allow you to avoid further lookups since they will be on the element you are handling the event for.
<button type="submit" class="btn btn-info send" data-quantity="45">Add To Order</button>
Then the logic would be like
$('.send').on('click', function(){
var $this = $(this);
console.log($this.data('quantity'));
});

Div text replace in other div

I want class="hotelname" echo text in class="a" and class="hotelname" is hide. is that possible? I used jQuery code but its not working.
<form id="" class="" method="GET" action="http://localhost/blue_bucket/county/" enctype="multipart/form-data">
<h2 class="a"></h2>
<?php
foreach ($query as $row)
{
?>
<h3 class="hotelname"><?php echo $hotelnames = $row->hotel; ?></h3>
<div class="book-it-btn">
<button type="submit" name="submit" class="btn btn-default">Apply</button>
</div>
</form>
<?php
}
?>
Jquery:
jQuery(".a").append(jQuery(".hotelname").html());
As per my understanding you want .hotelname class value to put in .a class then you want to hide .hotelname class element ?
Check the working snippet below
var hotelName = $(".hotelname").text();
$(".a").text(hotelName);
$(document).ready(function() {
var hotelName = $(".hotelname").text();
$(".a").text(hotelName)
$(".hotelname").hide();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="" class="" method="GET" action="http://localhost/blue_bucket/county/" enctype="multipart/form-data">
<h2 class="a"></h2>
<?php
foreach ($query as $row)
{
?>
<h3 class="hotelname">My hotel</h3>
<div class="book-it-btn">
<button type="submit" name="submit" class="btn btn-default">Apply</button>
</div>
</form>
<?php
}
?>

Submit button doesn't fire

My submit button suddenly stopped working. It submits data into a MySQL database.
While I was doing some design changes, it suddenly stopped working.
Are there any apparent errors/mistakes in the code below?
I'm a noob who's currently trying to learn some PHP and so on, so any help would be much appreciated. :)
<section id="moviesearch">
<!-- Section -->
<div class="subcribe2">
<div class="container">
<!-- Container -->
<div class="row">
<!-- Row -->
<div class="col-md-4">
</div>
<body ng-app="myApp">
<div ng-controller="MyCtrl" class="col-md-4">
<form class="form-watch-list-create">
<input required="required" placeholder="Movie title" type="text" class="form-control" typeahead="item.Title for item in getLocation($viewValue)" typeahead-loading="loadingLocations" typeahead-min-length="2" typeahead-wait-ms="1000" typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html" ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</body>
</div>
<!-- End Row -->
</div>
<!-- End Container -->
</div>
<script type="text/ng-template" id="customTemplate.html">
<a>
<span bind-html-unsafe="match.label | typeaheadHighlight:query" style="width: auto;"></span>
({{match.model.Year}})
<!-- <img ng-src="{{match.model.Poster}}" width="120"> -->
</a>
</script>
<div ng-controller="MyCtrl">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
</section>
Edit
Here's the code that sends data into the MYSQL database:
function addWatchList() {
if (isset($_GET['addtowatchlist'])) {
$name = $_GET['name'];
$conn = dbmysql();
$query ="INSERT INTO watch_list values(null,'{$name}','{$_SESSION['user_id']}','NOW()')";
if (mysqli_query($conn,$query)) {
$last = "SELECT * FROM watch_list WHERE created_by = '{$_SESSION['user_id']}' order by id desc limit 1";
$data = mysqli_query($conn,$last);
while ($row = mysqli_fetch_assoc($data)) {
include "_view.php";
}
}else {
echo "An error occurred. Please try again.";
}
exit;
}
}
I am looking to your code, missing attribute action="somefile.php" and method="get" if you are planning on submitting it using the form, you should put it or if you are planning on submitting your code using javascript you can use <form onsubmit="myFunction()"> That is what you are missing. I am not seeing you addtowatchlist name from your input so that php can catch it to your isset.
The submit button isn't inside the form.
You're missing the </form> tag to end the <form>. But you have </div> that matches the <div> just before </form>, so it's implicitly closing the <form> tag as well. Then you have <input type="submit"> after it. Since the submit button isn't inside the form, and has no form tag associating it with the form, it doesn't know which form it should submit when you click on it.
Try this:
<form class="form-watch-list-create">
<div ng-controller="MyCtrl" class="col-md-4">
<input
required="required"
placeholder="Movie title"
type="text"
class="form-control"
typeahead="item.Title for item in getLocation($viewValue)"
typeahead-loading="loadingLocations"
typeahead-min-length="2"
typeahead-wait-ms="1000"
typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html"
ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</form>
This puts the form around both columns.
Use the method attribute in form tag and end the form tag properly
<form role="form" method="get" class="form-watch-list-create">
<div ng-controller="MyCtrl" class="col-md-4">
<input
required="required"
placeholder="Movie title"
type="text"
class="form-control"
typeahead="item.Title for item in getLocation($viewValue)"
typeahead-loading="loadingLocations"
typeahead-min-length="2"
typeahead-wait-ms="1000"
typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html"
ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</form>

How can I set input type button holds different values

In the while loop I display the doctor name, specialist and payment. For each doctor I want to have a button to send him a message. Every doctor has an doctor id (did). But <input type="hidden" name="did" value="<?php echo $row3['did'];?>"/> it cannot hold the different values. What can I do to set different values using:
<input type="hidden" name="did" value="<?php echo $row3['did'];?>"/>
or:
<input type='button' name='contact' id='contact' value="Message" class='contact demo btn btn-primary'/>
Here is my code:
while($row3 = mysql_fetch_array($result3))
{
?>
<div class="col_1_of_4 span_1_of_4">
<div class="title-img">
<div class="title">
<img src="images/Crystal_Clear_user.gif" alt="">
</div>
<div class="clear"></div>
</div>
<h4 class=head>Doctor name: <?php echo $row3['dName'];?></h4>
<p>Specialist: <?php echo $row3['specialist'];?></p>
<p>Payment: <?php echo $row3['payment'];?></p>
<input type="hidden" name="did" value="<?php echo $row3['did'];?>"/>
<hr>
<div id='contact-form'>
more
<input type='button' name='contact' id='contact' value="Message" class='contact demo btn btn-primary'/>
</div>
<!-- preload the images -->
<div style='display:none'>
<img src='images/loading.gif' alt='' />
</div>
</div>
<?php
}
in order to store different values in same variable.
do it like this:
<input type="hidden" name="did[]" value="<?php echo $row3['did'];?>">
changes:
name="did"
into
name="did[]"
the variable "did" should be array.
to view the data :
var_dump($_POST["did"]);
You can't change button's value without affecting it's label. I'd suggest you to wrap every row's button into a separate form with a hidden field.
You should have 1 <form> tag per row. I believe simple:
<form method='post' action='/your-submit-script-goes-here.php'>
<input type='hidden' name='did' value='<?php echo $row3['did'];?>'>
<input type='button' name='contact' id='contact' value="Message" class='contact demo btn btn-primary'/>
</form>
will do the trick.
Don't forget to remove your current <form> tags.
So your code will be as follows:
while($row3 = mysql_fetch_array($result3))
{
?>
<div class="col_1_of_4 span_1_of_4">
<div class="title-img">
<div class="title">
<img src="images/Crystal_Clear_user.gif" alt="">
</div>
<div class="clear"></div>
</div>
<h4 class=head>Doctor name: <?php echo $row3['dName'];?></h4>
<p>Specialist: <?php echo $row3['specialist'];?></p>
<p>Payment: <?php echo $row3['payment'];?></p>
<hr>
<div id='contact-form'>
more
<form method='post' action='/your-submit-script-goes-here.php'>
<input type='hidden' name='did' value='<?php echo $row3['did'];?>'>
<input type='button' name='contact' value="Message" class='contact demo btn btn-primary'/>
</form>
</div>
<!-- preload the images -->
<div style='display:none'>
<img src='images/loading.gif' alt='' />
</div>
</div>
<?php
}

Categories