I am taking values form database in my php project. The values are taken and displayed fine. The problem is I have a button with each value. And I need a separate link for each button to see which data is selected. So that the functioning would depend on selected data. But each button gets the same name. This is my code.
<?php foreach($query as $row): ?>
<hr id="line">
<div id="wrong">
<!--<button id="connect" type="submit">Connect</button>-->
<span id="name">
<?php
if($row->fname==$this->session->userdata('catgry'))
{?>
<span class="match"> <?php echo $row->fname; ?> </span>
<?php
}
else {
echo $row->fname;
$this->session->set_userdata('re_use',$row->fname);
}
?>
 
<?php
if($row->lname==$this->session->userdata('catgry'))
{?>
<span class="match"> <?php echo $row->lname; ?> </span>
<?php
}
else {
echo $row->lname;
}
?>
</span>
<!-- <select name="back_up">
<option selected><?php echo $row->userid ?></option>
<option>a</option>
</select>-->
<button id="connect" type="submit" >Connect</button>
</div>
<?php endforeach; ?>
Please Help.
Keep a hidden input and set the value to it on click of the button. You can store the value to the radio button and use it inside the JS function.
function setSelected(elm)
{
document.getElementById('inputID').value = elm.value;
}
Outside the foreach loop.
<input type='hidden' name='SelectedUser' id='inputID' />
Inside loop, in the generated code for button.
<button id="connect" onclick='setSelected(this)' type="submit" value="<?php echo $row->userid; ?>">Connect</button>
In server side you can get the value using $_POST['SelectedUser'] when the page is submiited after selecting the user.
Related
I am using a form having select dropdown. I want to pass the value obtained from the selected option as a $_GET request in form action field but any ways to access it outside the foreach loop. Here is the code sample that I have written
<form id="dynamicForm" action="client-detail-dynamic.php?id=<?php echo $_GET['id']; ?>&r_id=<?php **PASS THE DROPDOWN VALUE ID HERE** ?>" method="post">
<select class="form-control" id="dynamicfy" name="dynamicfy">
<?php
$j = 0;
foreach($payment_data as $pd):
?>
<option value="<?php echo $payment_data[$j]->r_id; ?>"><?php echo $payment_data[$j]->fy; ?></option>
<?php $j++; endforeach; ?>
</select>
</td>
<td class="col-md-4">
<input type="submit" name="submit" id="submit" class="btn btn-sm btn-success">
</td>
</form>
NOTE: $payment_data is an array containing the table data with field names r_id, fy etc
I have two methods for this.
First method
Create a hidden field inside form element to store the value of id.Put form action null
<form id="dynamicForm" action="" method="post">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
On submit you will get two values
if(isset($_POST['submit'])){
$id=$_POST['id'];
$r_id=$_POST['dynamicfy'];
header("location: client-detail-dynamic.php?id=" . $id . "&r_id=" . $r_id . "");
exit();
}
Second method use javascript
<select class="form-control" id="dynamicfy" name="dynamicfy" onchange="rdrt(this.value)">
<script>
function rdrt(str){
id=<?php echo $_GET['id']; ?>;
if(str!=""){
location.href="client-detail-dynamic.php?id=" + id + "&r_id=" + str;
}
}
</script>
Rather than changing the page from FORM ACTION what you can do is pick the values and set them in url passed to header:location.
try this.
``<?php
if(isset($_POST['submit'])
{
$option = $_POST['dynamicfy'];
$id = $_POST['id']
header('location: http://client-detail-dynamic.php?id=$id,r_id=$option');
}
?>
<form id="dynamicForm" action="" method="post">
<select class="form-control" id="dynamicfy" name="dynamicfy">
<?php
$j = 0;
foreach($payment_data as $pd):
?>
<option value="<?php echo $payment_data[$j]->r_id; ?>"><?php echo $payment_data[$j]->fy; ?></option>
<?php $j++; endforeach; ?>
</select>
</td>
<td class="col-md-4">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>
<input type="submit" name="submit" id="submit" class="btn btn-sm btn-success">
" />
</td>
</form>
What you are trying to do is go somewhere based in the $_GET['id]. That's not possible server side as you have to FIRST make the request, then execute code. If your aren't trying to bring form data with you to this URL, then try this suggestion. However forget what I about not possible. you could do something like:
<?php
if(isset($_POST['submit-button'])) {
header("location: file.php?something=" . $_GET['id']);
}
// set the form action to nothing and add this to the same page the form is on
// and you can redirect based on the $_GET['id']
?>
To change value on selection of dropdown, You will need to use a jQuery on change of select box.
Please refer following code for same.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
jQuery(Document).ready(function() {
jQuery('#dynamicfy').change();
});
jQuery('#dynamicfy').change(function() {
jQuery('#dynamicForm').attr('action', 'client-detail-dynamic.php?id=' +<?php echo $_GET['id']; ?> + '&r_id=' + jQuery(this).val());
});
</script>
if you just want selected dropdown on the action page then You may also get selected dropdown on the action page with using $_POST['dynamicfy'] on action page "client-detail-dynamic.php"
I would like to know how we can store the value selected in an auto populated drop down (HTML5) into a variable, all in PHP. I want to obtain this value BEFORE clicking the 'Submit' button.
<?php
$i=1;
$j=0;
$result=pg_exec($pgsql_conn,"select * from crm.product_info order by 1");
while ($row = pg_fetch_assoc($result))
{
?>
<td><input type="checkbox"
id= <?php
echo $row['product_id'];
?>
value= <?php
echo $i;
?>
name= "prod[]">
<?php echo $row['product_name']; ?>
</td>
<td>
<input type="button" value=<?php echo "$".$row['product_value']; ?> id="but">
</td>
<td>
Quantity
<select class="select" name="qty[]">
<?php echo "qty".$j; ?>
</select>
</td>
<td>
Amount
<input
id="amt"
type="text"
readonly
value=<?php
echo $row['product_value']
?> >
</td>
<?php
}
?>
I require the SELECTED value of 'quantity'.
This is how i auto populate the quantity. It is from 0-100.
$(document).ready(function() {
for(i=0;i<=100;i++)
{
$(".select").append("<option value=\""+i+"\">"+i+"</option>");
}
});
First of all, PHP is server side, so it can't handle changes made in the browser until a request is send to it. You will have to use javascript to know selected value in real time before submitting the form. You can manipulate this value using javascript. But if you really need to do some PHP with the selected value, your solution is to use Ajax for this.
I have built a inquiry form on my website, the idea is instead of mailing me each time a user submits a query it is added to my database which I can then go and view via my backend system
Each query will be listed one by one with a text-area contact form below it allowing me to reply to each query individually
So far I have this (sorry it's a bit messy)
foreach ($listings as $row){
$loop.= "<h3 class='text-center'>".$row['question']."</h3>";
$loop.= "<p>".$row['message']."</p>";
$loop.= "Name: <b>".$row['name']."</b>";
$loop.= "<span class='pull-right'>Email: <b>".$row['email']."</b><br></span>";
$loop.= "<div class='clearfix'></div>";
if(isset($row['website'])){ $loop.="Website: <b>".$row['website']."</b>"; }
$loop.= "<span class='pull-right'>Date: <b>".$row['date']."</b></span>";
$loop.= "<form name='submit-response' method='POST'><fieldset>";
$loop.= "<div class='form-group'> <label for='Message".$counter."'>Your Message</label> <textarea id='Message".$counter."' name='Message".$counter."' class='form-control' rows='5'></textarea> </div>";
$loop.= "<button type='submit' name='submit".$counter."' class='btn btn-default btn-block'>Reply</button>";
$loop.= "</fieldset></form>";
}
Before that is a foreach loop and the start of the oh and $counter is set to nill
What I want is for each contact form to be unique so when I click send on one of the queries it will be sent and removed so I can send another, the only issue I am having is working out how I will work out if a submit has been hit, and which submit has been hit
The code will need to workout which button has been hit and depending on which button it will then mail() to the recipient
I'm quite stuck on this one and I'm not sure of the best course of action so any advice is really appreciated
Luke
If you click a submit button inside a <form> tag, then only that form will be submitted.
You could include a hidden field with the ID of the row in it. That way you could get rid of the $counter variables altogether.
Also if you plan on just echoing out the $loop html, I wouldn't recommend storing the HTML in a PHP variable.
<?php
foreach ($listings as $row)
{
?>
<h3 class="text-center"><?php echo $row['question']; ?></h3>
<p><?php echo $row['message']; ?></p>
Name: <b><?php echo $row['name']; ?></b>
<span class="pull-right">Email: <b><?php echo $row['email']; ?></b><br></span>
<div class="clearfix"></div>
<?php
if(isset($row['website']))
{
?>
Website: <b><?php echo $row['website']; ?></b>
<?php
}
?>
<span class="pull-right">Date: <b><?php echo $row['date']; ?></b></span>
<form action="" name="submit-response" method="POST">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<fieldset>
<div class="form-group">
<label>Your Message</label>
<textarea name="Message" class="form-control" rows="5"></textarea>
</div>
<button type="submit" class="btn btn-default btn-block">Reply</button>
</fieldset>
</form>
<?php
}
?>
add a unique id to your database table, and put it in a hidden input.
Give each form a id, and possibly each submit button a unique name. This way you can easily determine which submit button was hit, or which form was submitted, and remove it or process it via javascript.
$('form').each(function() {
$(this).submit(function(event) {
event.preventDefault();
// Add AJAX code here
$(this).remove();
});
});
Of course that was pseudo-code.
<?php $result2 = db_getsub( array('campaigns_id' => $SYS_campaign) );
if (!is_null($result1)){
while ( $row = $result2->fetch_object() )
{
?>
<div class="item">
<div style="text-align: left;">
<div class="choicetitle">
<input type="radio" id="Radio1" checked="checked" title="" name="subtype" value="<?php echo $row->subid; ?>" /><?php echo $row->sub_name; ?></div>
<div class="choicedesc descrip">
<?php echo $row->gavdescrep; ?> </div>
<!-- <div class="choicelesmer">
Les mer
</div>-->
<div class="choicedesc1">
<?php echo $row->gavdescrep; ?> </div>
</div>
<span><?php
$value=$row->price;
if($value != 0)
{
echo $row->price; ?>,-
<?php }else { echo ""; } ?> </span>
<span class="bgcolor discprice">
<?php echo $row->pricediscount; ?>,-
</span>
</div>
<?php } } ?>
I want to show default 1st radio button checked now its showing last button checked as it is coming dynamically any help???
You are using checked="checked" for all your radio buttons in the loop. So, last assigned radio button retains the checked property. To solve the problem, Conditionally set checked="checked" only for the first looping
if(loopingFirstTime){
$checked = ' checked="checked" ';
}else{
$checked = ' ';
}
and then ....
<input type="radio" name="radio1" '.$checked.' />
Not tested this .... please check the syntax
Use checked=checked property to make a radio button checked by default:-
Blue
http://www.w3schools.com/jsref/prop_radio_defaultchecked.asp
And to handle that with jquery:-
jQuery("#radio_1").attr('checked', 'checked');
How to check a radio button with jQuery ?
Like the other guys have already said, you have only implemented one radio button.
You can try jsfiddle.net for example to try out you code and quickly see your changes.
Btw: I'm not quite sure what you're trying to do in you last row: Is that a leftover or those this belong to the wrapped-around code?
I have a page that is created dynamically from the information that a user has submitted on a previous page. For example on page 1, the user inputs some department names. They can enter as many as they want. On page 2, multiple sections are created for each department that was entered on page one. Inside each of these sections are going to be forms, I currently have it set up so that the names of the forms are created using a variable $a. I need to know how to post these items once the submit button in each section is clicked. I have tried several different ways and nothing is working. I want it so that ONLY the items with the same $a value as the submit button's $a get posted.
$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);
$a = 0;
while ($departmentRow = mysql_fetch_array($departmentResults)) {
$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;
echo "
<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
";
$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>
<br />
<input type="hidden" name="bdep<?php echo $a;?>" value="<?php echo $b_departmentID; ?>" />
Add Area:
<select name="dep_area<?php echo $a;?>" type="menu">
<option value=""></option>
<?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
<option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><? php echo $areaRows[ 'area_name'] ?></option>
<?php }
?>
</select>
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
<?php
echo "</div>";
}
?>
*EDIT I need everything to be in one form because the point of the page is to add up all of the values that will be inserted into each of the individual sections later. *
**EDIT 2 :
I figured it out using #dirt 's jquery suggestion.
HTML:
$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);
$a = 0;
while ($departmentRow = mysql_fetch_array($departmentResults)) {
$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;
echo "
<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
<div id=$a>
";
$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>
<br />
<input type="hidden" name="bdep<?php echo $a ?>" value="<?php echo $b_departmentID; ?>" />
Add Area:
<select name="dep_area<?php echo $a ?>" type="menu">
<option value=""></option>
<?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
<option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><? php echo $areaRows[ 'area_name'] ?></option>
<?php }
?>
</select>
<button type="submit" name="areaSub" value="<?php echo $a ?>" />Add</button>
<?php
echo "</div></div>";
} ?>
jQuery:
<script>
$(document).ready(function() {
$('#<?php echo $a ?>').submit(function() {
.post("include/action.php")
});
});
</script>
PHP:
if(isset($_POST['areaSub'])) {
$areaval = intval($_POST['areaSub']);
$area = mysql_real_escape_string($_POST["dep_area".$areaval.""]);
list($area_id, $area_name) = explode(',', $area, 2);
$bdep = mysql_real_escape_string($_POST["bdep".$areaval.""]);
echo $areaval;
echo $area_name;
echo $bdep;
}
EDIT: If its a single form with multiple sections and you don't want to trim out unwanted form data after the POST then I think you must use jQuery to trim the form values prior to the post to the server, so see my jQuery portion below for a crued example of how you would go about only posting the data for the selected button.
Short answer would be to use the Name/Value attributes of the submit button and evaluate that once posted. Multiple buttons with the same name can have different values and the values don't have to be the labels.
Example:
<form id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<form id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<?php
# _POST:
if (isset($_POST['submit']) && $_POST['submit'] == '1') {
echo 'Department 1';
}
if (isset($_POST['submit']) && $_POST['submit'] == '2') {
echo 'Department 2';
}
?>
You could also use jQuery to get all elements contained in a certain Div ID. Something like (this is rough idea):
<div id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
</div>
jQuery:
$('#<?php echo $a; ?>').submit(function() {
do_something_with_form_$a
});
I'm not sure if I understand completely... but the issue you're having is that, when a user hits the 'submit' button for a given set of values (a given department), it submits ALL of the data on the page? And you only want the input fields for that specific area submitted?
Section off forms with the tag:
<form> </form>
So, you'll have multiple areas:
while(...) {
<form method="..." name="form $a">
<input name="... $a">
<input name="... $a">
...
<submit>
</form>
}
(Obviously this is pseudocode, adjust accordingly)