I've a form using jquery ui ,
I must do a radio choice wich takes the values from DB so i tried this :
//function wich reutrn applicable ratio for the pricing
function getAvailableCoeff(){
echo "<td>Coeff : </td>";
echo "<td><div class='radio'>";
$req = "SELECT coef,id_type FROM type_client WHERE valide =1 GROUP BY coef";
$res = mysql_query($req);
while($row=mysql_fetch_array($res)){
$id = 'coeff'.$row['id_type'];
$value = $row['coef'];
$input = "<label for='$id'>$value</label>";
$input .= "<input type='radio'id='$id' name='coeff' value='$value'/>";
echo $input;
}
echo "</div></td>";
}
Nothing very difficult no ? But when i sent it to integration server i saw enormous radio buttons ( they are 3 atm)!
i let you check it here
(on the screen the "1","2","3","4","quadri","5","6" radios buttons are hardcoded with this syntax : <input type="radio" id="spT_radio-coul-1" value="1" name="nbCoul" /> <label for="spT_radio-coul-1">1</label>
Moreover the radio buttondon't pass in the form sumbission , $_POST['coef'] is still empty..
So i take a look to the html and .. WTF ?
Each radio button get this kind of code :
<label for="coeff5" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
<span class="ui-button-text">
<span class="ui-button-text">
<span class="ui-button-text">
<span class="ui-button-text">
<span class="ui-button-text">
<span class="ui-button-text">0.66</span></span></span></span></span></span></label>
So jquery auto generates all theses spans but how to remove them in order to get normals radio buttons ?
Have anyone encountered thsi kind of problems with jquery ui ?
TEMPORARY (ugly) SOLUTION :
.ui-button-text-only .ui-button-text {
padding:2px;
!important;
}
FINAL SOLUTION (Thx SuperScript) :
//function wich reutrn applicable ratio for the pricing
function getAvailableCoeff($form){
echo "<td>Coeff : </td>";
echo "<td><div class='radio'>";
$req = "SELECT coef,id_type FROM type_client WHERE valide =1 GROUP BY coef";
$res = mysql_query($req);
while($row=mysql_fetch_array($res)){
$id = $form.'coeff'.$row['id_type'].$i;
$value = $row['coef'];
$input = "<label for='$id'>$value</label>";
$input .= "<input type='radio' id='$id' name='coeff' value='$value'/>";
echo $input;
$i++;
}
echo "</div></td>";
}
I pass a string value for each call , like that the id's are really unique !
Solved! The problem is using duplicate ids.
On the page, all of your <input> use the same sets of ids (#coeff5,#coeff2,#coeff1). jQuery UI uses those ids to add the spans, so the duplicates are wrecking it.
ids must be unique.
I made some test cases for this:
Not working: http://jsfiddle.net/PGcL4
Working JSFiddle.
So, you would change your PHP code to:
//function wich reutrn applicable ratio for the pricing
function getAvailableCoeff(){
echo "<td>Coeff : </td>";
echo "<td><div class='radio'>";
$req = "SELECT coef,id_type FROM type_client WHERE valide =1 GROUP BY coef";
$res = mysql_query($req);
$i=0;
while($row=mysql_fetch_array($res)){
$id = 'coeff'.$row['id_type'].$i;
$value = $row['coef'];
$input = "<label for='$id'>$value</label>";
$input .= "<input type='radio' id='$id' name='coeff' value='$value'/>";
echo $input;
$i++;
}
echo "</div></td>";
}
That makes all of the ids unique like required.
Related
The context is: e-commerce, the problem stands in adding items to the cart.
I created a while loop to iterate through each item my query returns and each item has a button that redirects to a "info on the item" page. My problem is that since there's a loop, the values to submit (e.g. the ID of the item) is overloaded and every button submits the values of the last item.
I can pass all the IDs of all the plants but i have no idea how to, in the "item detail" page, to show the correct item among the array.
lista-piante.php: (summarized)
<?php session_start();
// connect to the database
$connessione = new mysqli('localhost', 'root', 'root', 'mio');
//query
$user_check_query = "SELECT Pianta.NOME as nome, PIANTA.ID as pid, Item.PREZZO as prezzo, Item.ID as id
FROM Item, Pianta WHERE Pianta.ID = Item.PIANTA";
$result = mysqli_query($connessione, $user_check_query);
if($result->num_rows > 0) {
echo"<h3>Lista delle nostre piante</h3>";
echo"<ul class=\"plant-flex\">";
// loop through records
while($row = $result->fetch_array(MYSQLI_ASSOC)){
echo"<form method='get' action='../html/details-pianta.php'>";
echo"<li>";
echo"<div class='plant-preview'>";
echo"<div class='plant-preview-description'>";
//dichiarazione variabili (per leggibilitĂ )
$nome= $row['nome'];
$pid = $row['pid'];
// PRINT NAME
echo"<div class='plant-preview-description-name'>";
echo "<p class='bold'>" . $nome . "</p>";
echo "<input type='hidden' name='name' value='$nome' />";
echo"</div>";
echo "<input type='hidden' name='pid[]' value='$pid' />";
echo"<div>";
echo"<button type=\"submit\" class=\"btn\" name=\"details_plant\">Dettagli" . $item . "</button>";
echo"</div>";
echo"</li>";
echo"<form/>";
}
$result->free();
}
echo "<ul/>";
$connessione->close();
details-pianta.php: (summarized, it will contain the style of the page)
<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">
<head> <!-- meta tag and other stuff --> </head>
<body>
<div>
<?PHP include('../php/dettagli-pianta.php'); ?>
</div>
</div>
</body>
</html>
dettagli-pianta.php: (summarized, it should contain the info of each item)
<?php
session_start();
// connect to the database
$connessione = new mysqli('localhost', 'root', 'root', 'mio');
$pid = mysqli_real_escape_string($connessione, $_GET['pid']);
//but pID is either the last item's id, or an array with all items' ids, so i can't chose the only one i want
//the URL shows always more pIDs (from the lista-piante.php get form)
$user_check_query = "SELECT * FROM Pianta, item WHERE Pianta.ID = '$pid' ";
$result = mysqli_query($connessione, $user_check_query);
if($result->num_rows > 0) {
echo"<h3>Dettagli della pianta</h3>";
echo"<ul>";
//loop thought query records
while($row = $result->fetch_array(MYSQLI_ASSOC)){
echo"<form method='post' action='../php/add-carrello.php'>";
echo"<li>";
echo"<div>";
//dichiarazione variabili (per leggibilitĂ )
$nome =$row['NOME'];
//$genere= $row['GENERE'];
//$specie= $row['SPECIE'];
//etc etc
// PRINT NAME
echo"<div>";
echo "<p class='bold'>" . $nome . "</p>";
echo"</div>";
//echo"<div>";
//echo "<p class='bold'>" . $specie. "</p>";
//echo"</div>";
//echo "<input type='hidden' name='pid' value='$pid' />";
echo"<div>";
echo"<button type=\"submit\" class=\"btn\" name=\"add-carrello\"> Aggiungi al carrello</button>";
echo"</div>";
echo"</li>";
}
$result->free();
}
echo "<ul/>";
$connessione->close();
There is an error with form closing, but... I have a feeling you are complicating it for yourself by using all those forms/buttons.
I would clean up the code and get rid of the form/button scheme altogether, and use simple a href links with ?id=$pid
Something like this (shortened, if needed post a comment and I can expand):
while($row = $result->fetch_array(MYSQLI_ASSOC)){
echo "<li>";
echo "<div class='plant-preview'>";
echo "<div class='plant-preview-description'>";
//dichiarazione variabili (per leggibilitĂ )
$nome = $row['nome'];
$pid = $row['pid'];
// PRINT NAME
echo "<div class='plant-preview-description-name'>";
echo "<p class='bold'>" . $nome . "</p>";
echo "</div>";
echo "<div>";
echo "<a href ='../html/details-pianta.php?pid=$pid'>Dettagli " . $nome . "</a>";
echo "</div>";
echo "</li>";
}
You can use CSS later to make your a-href link look like a button, add image, or play with it any way you like.
Then in the product details page (dettagli-pianta.php) use your product id pretty much same as you already do:
$pid = $_GET['pid']
...
$user_check_query = "SELECT * FROM Pianta WHERE Pianta.ID = '$pid' ";
Here is one Stack Overflow that talks about using link instead form:
How send parameter to a url without using form in php?
Edit (mistakes found, more suggestions, in case you still want to keep forms/buttons):
you did not close ?> in your examples, but ok, probably just copy/paste here
you have $item variable that isn't defined, and not pulled from SELECT query, so I can only assume that is supposed to be $nome (or you deleted something to make it shorter in question and forgot about it)
you also probably wanted a space like this (note space after Dettagli)
echo "<button type='submit' class='btn' name='details_plant'>Dettagli " . $nome . "</button>";
not sure why you keep files in folders "../html/" and "../php/" when both are with extensions .php, could be confusing for others later working on your code, or ... maybe it's just me
you have <?php session_start();?> then under it you include a file that also has <?php session_start();?> ... no need for one of them. Since "details-pianta.php" is in folder "html" I guess you can remove it from there, as you don't need session just to include another PHP file
in form you submit name='pid[]' when you should just use name='pid' (no brackets), like this:
echo "<input type='hidden' name='pid' value='$pid' />";
in "dettagli-pianta.php" you have a select like this: $user_check_query = "SELECT * FROM Pianta, item WHERE Pianta.ID = '$pid' "; where you name two tables but don't join them, and I can only assume it should be $user_check_query = "SELECT * FROM Pianta WHERE Pianta.ID = '$pid' ";
be careful of uppercase/lowercase in database and column names, try to standardize
in one line you echo with mixed quotes something like echo "<bla name='bla'>" and in next you escape quotes like echo "<bla name=\"bla\">" ... make it easy on yourself and - standardize. Unless absolutely necessary, first way (mixing single and double quotes) is preferable, and only when you have something really complicated to echo like mix of HTML+JavaScript, then resort to escaping quotes
And finally, but actually a direct answer to your issue - you closed your form in the wrong way (yeah, had to look real hard to see it):
echo"<form/>";
You need to have (space is optional, reads better):
echo "</form>";
Non the less, as others too have already commented, using forms just to get buttons is an overkill. Try using simple links (<a href> elements) and style them with CSS to your liking, making them look like buttons...
And last, but... I believe it to be important... Try not to mix Italian and English in code (including database structure). It is obvious you are just learning but havin database called "mio" with table "pianti" and column "prezzo" ... then use something like "item". Also, names of files like "details-pianta", then you have another file "detaggli_pianta", and in button name you use "details_plant", it is huge mashup of two languages. you seem to be fluent English speaker judging from your question, so - why not use English everywhere in code? Name your database "my_first_web_store", name your table "plants", name your column "price", name your PHP files "plant_details" (or details_plant, whatever), use comments in English in your code (nobody but you and other developers see that). You will be thankful later when your plant-selling company expands worldwide, and when you will have multi-language webshop, and maybe 3 more developers working from India and US, all working on same project... :)
Have fun learning!
I have the following button echo'd:
<input type='submit' name='delete_$id' value='x' title='Delete message' />
$id is gathered from here:
$get_messages = mysqli_query ($connect, "SELECT * FROM private_messages WHERE message_to = '$username' OR message_from='$username' AND
DELETED ='no' ORDER BY id DESC");
while ($get_msg = mysqli_fetch_assoc($get_messages)){
$id = $get_msg['id'];
}
I want to get the id of the message the x is assigned to and then execute this:
if (#$_POST['delete_' . $id . '']){
echo "Deleted";
}
But the echo is not displaying on my page, meaning the code is faulty. I have checked numerous times, and cannot figure out why it isn't working?
Edit:
I am simply trying to add a delete button which on click will SET the deleted column in my table to yes.
Here is the complete code where the delete button is found:
<?php
echo "
<div class='parent'>
<div class='msg_prof'>
<img class='img-rounded' src='/user_data/profile_pics/$my_pro_pic'/>
</div>
<div class='new_msg_from_user'>
<p><b style= 'color: red;'> You said:</b> $msg_body</p>
<span class='faded'>$date </span>
<input type='submit' name='delete_$id' value='x' title='Delete message' />
</div><hr/>
</div>";
?>
My thinking behind this is that, I can get the id of a post using the $id variable, and assign that to the name. Then by using if (#$_POST['delete_' . $id . '']) I can perform the query based on the $id - for now I am trying to echo a message, just for testing purposes. This is all the code I have for this functionality.
you forgot the php tag.
<input type='submit' name='delete_<?php echo $id ?>' value='x' title='Delete message' />
First of all, look what you are displaying the line:
<input type='submit' name='delete_$id' value='x' title='Delete message' />
e.g. name should/must be displayed as name='delete_1' or name='delete_9' or any other string having a number/digit at the end of its name. Generically said: name='delete_anyNumber'
For that you should have the Fetched Records of messages(those which you want to displayed and have a cross button just for a good presentation of NORMS) in PHP File and iterate through each result and in iteration-loop, echo all the messages with X buttons like:
echo "<input type='submit' name='delete_" . $ResultVariableHoldingRows['id'] . "' value='x' title='Delete message' />";
THE REASON BEING: (You'll have to display it correctly so that when you POST the form, you must be able to get the exact POSTED value that needs to be DELETED, so that you don't have to iterate to check which row needs to be DELETED.)
After you get the above problem resolved, follow below instructions:
Access the value, in PHP file that is called after Submitting:
$_POST['\'delete_' . $id . '\'']
SUGGESTION: Use AJAX-Calls to DELETE the respective messages as the many forms on a single page will not be a good an approach and do some good search on AJAX before posting a question
As in the comment mentioned you have to iterate through the global POST array.
<?php
function getRecordIdFromKey($sKey)
{
$sId = str_replace("delete_", "", $sKey);
$iReturn = -1;
if (strlen($sId) > 0 && is_numeric($sId))
{
$iReturn = (int) $sId;
}
return $iReturn;
}
foreach ($_POST as $sKey => $mValue)
{
$iId = getRecordIdFromKey($sKey);
if ($iId >= 0)
{
// fire up your delete query
}
}
I am working on an attendance module. Initially, it will show the list of all students along with a dropdown having options Present/Absent. The faculty member will choose Present/Absent accordingly & submit the same.
I am having problem in storing the corresponding values to the DB.
$sql = "select a.student_id, r.student_name, r.section, r.group_name, a.$subject from result.$batch r, attendance.$batch a where a.student_id = r.student_id AND r.section='$section'";
$c = 1;
$result1 = $result->query($sql);
if ($result1->num_rows > 0)
{
while($row = $result1->fetch_assoc())
{
echo '<tr>';
echo "<td>$c </td>";
echo "<td>{$row['student_id']}</td>";
echo "<td>{$row['student_name']}</td>";
echo "<td>{$row['section']}</td>";
echo "<td>{$row['group_name']}</td>";
echo "<td>
<select class='dropdown' id='attend' name='attend[$c]' > <option value='1'>Present</option> <option value='2'>Absent</option>
</td>";
echo '</tr>';
++$c;
}
}
else
{
echo "No Results Found";
}
Can someone please help me with the updation code. Updation is to be made in the table $batch (batch is a variable containing Table Name to use) and column $subject (contains variable name).
well, you can do one thing.when user clicks on the check-box to mark absent or present, save it into an array using Javascript like :
onclick='array.push(this.id);'
this would push the id of current element to an array in Javascript.
When you finally submit the form, just do this,
onsubmit="passValues();"
in script tag, do this
function passValues()
{
var x = array // the array to which elements were pushed
document.getElementById('someBlankElement').innerHTML = "<input type='hidden' value = 'display array here' name='get_this_name_through_php_when_form_submits' >"
}
and done !
Scenario:
I have a radio button where when the user click the radio button it will redirect to another page....
So here's my code:
<?php
$radio = mysql_query("SELECT fldNetname FROM tbldata WHERE fldMonth = '$get_month' AND fldWeek = '$get_week' GROUP BY fldNetname ORDER BY fldNetname");
while ($row = mysql_fetch_array($radio)){
echo "<div><input type='radio' name='playRadio' class='chk_boxes1' value='" . $row['fldNetname']."' " .($_POST['playRadio'] == $row['fldNetname'] ? 'checked' : '') . " onClick=\'this.form.action='looptime_utilization_process.php'; this.form.submit()\' >";
echo $row['fldNetname'];"</div>";
}
?>
When I try to run it there is no error yet its not directing to the page I want.
IS there a way that the radio button will be also like a button that can be proceed to another form?
Thanks.
I have corrected all your HTML and PHP mistakes:
<?php
$radio = mysql_query("SELECT fldNetname FROM tbldata WHERE fldMonth = '" . mysql_real_escape_string($get_month) . "' AND fldWeek = '" . mysql_real_escape_string($get_week) . "' GROUP BY fldNetname ORDER BY fldNetname");
while ($row = mysql_fetch_array($radio)) {
?>
<div>
<input type="radio" name="playRadio" class="chk_boxes1" value="<?php echo $row['fldNetname']; ?>"<?php echo ($_POST['playRadio'] == $row['fldNetname'] ? 'checked=" checked"' : ''); ?> onclick="this.form.action='looptime_utilization_process.php'; this.form.submit();">
<?php echo $row['fldNetname']; ?>
</div>
<?php
}
What I changed:
added mysql_real_escape_string(); - better security if your variables are not already escaped
change echo string concatening to close / open <?php tags
changed onClick to onclick (all attributes should be written in lower case in most modern standarts)
changed checked to checked="checked"
corrected wrong quotes in onclick attribute
<form action='looptime_utilization_process.php'>
<input type='radio' name='playRadio' class='chk_boxes1' onClick='form.submit();' >
<form>
Use Javascript
if(document.getElementsByID("playradio").value==<ur value>)
{
param="<ur parameters>";
xmlhttp.open("GET", "looptime_utilization_process.php?".param ,false);
xmlhttp.send();
}
Try this
$trgt=$_POST['playRadio'];
$radio = mysql_query("SELECT fldNetname FROM tbldata WHERE fldMonth = '$get_month' AND fldWeek = '$get_week' GROUP BY fldNetname ORDER BY fldNetname");
while ($row = mysql_fetch_array($radio)){
$v=$row['fldNetname'];
echo "<div><input type='radio' name='playRadio' class='chk_boxes1' value='$v'"
.($trgt == $v ? 'checked' : '')
.' onclick="o=document.forms[0];o.action=\'looptime_utilization_process.php\'; o.submit()" >';
echo "$v</div>";
}
You had some issues with the quotes in your onClick section. Also this does not work there in the way you wanted. I replaced it by document.forms[0], assuming that it is the first form on that page you want to submit and of course, also assuming that the radio buttons are actually inside <form> tags! Change this as required ...
And, of course: placing the action=... context in the onclick section of the radio buttons really only makes sense if the original action of the form pointed to something else which needs to be there for other use cases. Otherwise follow Gary Hayes' advice and put the action attribute into the <form>...</form> tags.
example.php ~ when i check the checkbox in example.php webpage.. then i click the print button, the value will be pass on example1.php
if($items) {
foreach($items as $i)
{
print"<input name='checkbox[]' type='checkbox' id='checkbox[]' value='$i->logi_id'>";
}
}
echo "<a href='example1.php?checkbox=" . $checkbox . "'><input type='button' name='print' id='delete' value='Print'></a>"
?>
example2.php
<?php
$keyword = $_REQUEST['keyword'];
$keycateg = $_REQUEST['keycateg'];
$print = $_REQUEST['print'];
$checkbox = $_REQUEST['checkbox'];
$count = count($_REQUEST['checkbox']);
if($print){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "select * FROM sampleDB WHERE logi_id='$del_id'";
$result = mysql_query($sql);
}
}
?>
Simple words ---
checkbox=<?php echo $checkbox; ?>
but dont rely on user inputs,...sanitize the data
If i understand your question correctly you want to submit the form using a link, prefered way would be to style a submit button as a link, but you could ofc use javascript to submit the form using a link. you can find a more indepth explanation here
http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
I guess his question is how he can evaluate the current value of the checkbox and use it in a a href "GET" encoded style.
I would recommend not using a href, but using another form with post method "get" (that encodes the url for you).
Else your are stuck with javascript I believe.
$checkbox_string = htmlentities(http_build_query(array('checkbox'=>$checkbox)));
echo "<a href='example1.php?$checkbox_string'>...</a>"