PHP: Radiobutton will direct to another page without using a button/submit - php

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.

Related

Assigning variable to input type[submit] name and using it for PHP query

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
}
}

jquery ui creates a lot of undesirable spans

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.

Delete selected row from database

I would like to delete a row from mysql database. The row I would like to delete is the 'selected item' in the combobox.
My code so far:
$sql = "SELECT data_id FROM projects";
$result = mysql_query($sql);
echo "<select name='data_id '>";
echo "<option selected>Please select...</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['data_id '] . "'>" . $row['data_id'] . "</option>";
}
echo "</select>";
?>
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
<?php
//delete selected item from projects table...
?>
The combobox is showing all the items from my projects table. When I select an item, then click on 'delete' I would like to delete that item from the table.
Any help will be appreciated.
I did not get why do they suggest you to use ajax, while you can make simple form
<form method="post" action="">
<tr><td>
//SOME PHP where you obtain from the database the data_id (this you have done already)
<option value='" . $row['data_id '] . "'>" . $row['data_id'] . "</option>
//
<input type="submit" name="delete" value="Delete" id="delete">
</td></tr>
</form>
and on same page at the top before HTML
<?php
if(isset($_POST['delete']) {
//and now mysql DELETE FROM .... WHERE id = $_POST['data_id']
//dont forget to escape and use mysli instead of mysql
}
?>
Btw. dont use mysql_* its deprecated, + escape everything in mysql + dont use fetch_array when you dont need an array, fetch_assoc is fine ;) Hope that helps.
Your logic should be:
At top of script before html, check for POST values from form
if they exist, use the variables to delete the appropriate database value
use header('location') back to the same page to reload the page (so value is removed from form below)
where you have your select box, make that a form, action= same page, method = post
tomorrow I will post example code if you have not worked it out by then
first off all you are using type submit and by that you need a form,and when you use option you need to set a value or the option
1
2
3
if you want to delete a particular row in the database and let say the id is 1 so it is the first row do it like this
if(isset($_GET['to_be_deleted']))
{
$id=mysql_real_escape_string($_GET['to_be_deleted']);
mysql_query("DELETE FROM name_table where id='$id'");
}
if my answer is not good please ask me with more details

A while-loop of a drop-down menu box, and having the submit button be outside of this while-loop

I currently have a drop-down menu box that takes an element from a column from my mySQL database and displays it in the drop-down box. The user can also select from five of the pre-determined options. When submit is clicked, the changes get reflected in the database.
There is no issue with my code, but I would like to make it more intuitive for the user. At the moment, upon clicking submit, the page refreshes, and the page loses its scroll position (once refreshed, the page goes to the top).
How would one make a submit button outside of the while-loop? I want to have the submit button at the bottom of the page, which would mean it will be outside of the iterating while loop, right? How could I do this?
Attached is my code:
<?
if (isset($_POST['formSubmit'])){
$rating = $_POST['rating'];
$accountID = $_POST['accountID'];
mysql_query("UPDATE Spreadsheet SET rating='$rating' WHERE accountID='$accountID'");
}
while ($row = mysql_fetch_array($query)){ ?>
<form name ="rating" method ="POST" action ="" > <?
echo "<input type = 'hidden' name = 'accountID' value = '" . $row['accountID'] . "' >";
?>
<select name="rating">
<? $values = array('0 - No rating','1 - Very Bad','2 - Bad','3 - Average','4 - Above Average');
for ($i =0; $i < count($values); $i = $i + 1){
echo "<option value = \"$i\"";
if ($row['rating'] == $i) {
echo "selected=\"selected\"";
}
echo ">" . $values[$i] . "</option>";
}
?>
</select>
<input type ="Submit" name ="formSubmit" value ="Submit" />
</form>
}
I suggest you work this out with AJAX. Instead of submitting it as form, send it as a AJAX request. Something like:
$('select[name="rating"]').change(function () {
var rating = $("select[name="rating"] option:selected").val();
$.post(
'YOUR-URL.php',
{'rating' : rating},
function(response) {
//do what you got to do
}
)
});

how to pass a checkbox value using <a href

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>"

Categories