Where can i implement a php code in joomla? - php

So I'm doing this website with Joomla and I don't have much experience with Joomla.
I just want to get the values of some checkboxes and print them into a text area.
I have the script already, I just need to implement it.
Here the raw Html code of the checkboxes:
<div class="checkboxShop">
<form action="" method="post">
<input type="checkbox" value="1" id="checkboxShopInput" name="shop" />
<label for="checkboxShopInput"></label>
</form>
</div>
And Here is the raw Html code of the submit button:
<form action="/Flex/index.php/shop" method="post">
<input type="submit" value="Buy">
</form>
All I want is to get all the values of the checkboxes and add them to a textarea in index.php/shop. I wrote this code to get the values but i don't know where to add this:
<?php
if(!empty($_POST['shop'])) {
foreach($_POST['shop'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
echo "<input type='text' name='message' value='" . $check . "'>"
}
}
?>
I've read that i need this but I'm not sure how: <?php include "name_of_script.php";?>
Thanks for the help

Related

HTML form using radio button to pass PHP array on

I have created a simple HTML form using radio input, consisting of three inputs, namely ONX, BUDAIR and ECC accordingly. When ONX is clicked and submitted, I need it to pass the array $onx on. I have only included the $onx array for simplicity.
I have managed to get it to work using http_build_query, but I want it to be in a form.
<!DOCTYPE html>
<html>
<body>
<h2>Radio Buttons</h2>
<?php $onx=array("onx_service",2,3,4,5);
print_r($onx); ?>
<br />
<!--<form action="radio.php">
input type="radio" name="service" value="onx"> ONX<br>
<input type="radio" name="service" value="bud"> BUDAIR<br>
<input type="radio" name="service" value="ecc"> ECC<br>
<input type="submit" value="Submit">
</form> -->
<?php echo "<a href='radio.php?" . http_build_query($onx) . "'>Select
ONX service</a>";
echo "<br>";
echo $onx[0] . "<br>";
echo $onx[1] . "<br>";
echo $onx[2] . "<br>";
echo $onx[3] . "<br>";
echo $onx[4] . "<br>";
?>
</body>
</html>
When either of the three radio inputs are clicked, it should pass on the relevant array.
When I manage to get the code working correctly I will add it into the correct file. The "http_build_query($onx)" potion of the code above is a sample of what I am trying to achieve within the form using radio inputs.

this.form.submit() with PHP isset?

echo "<form method=\"post\" action=\"settings.php\" onchange=\"this.form.submit()\">";
echo "Announce New Files: <input type=\"radio\" name=\"announcefiles\" value=\"On\" $checkon1> On";
echo "<input type=\"radio\" name=\"announcefiles\" value=\"Off\" $checkoff1> Off<br>";
echo "</form>";
I am trying to get this form to submit when one of the radio buttons is pressed but I'm not sure how to catch the submission.
for example, normally with a submit button you would use something along the lines of if(isset($_POST['submit'])) but I'm not sure how to do it if the form auto submits.
Add hidden input field like:
<input type="hidden" name="action" value="submit" />
Then in PHP check:
if(isset($_POST["action"]) && $_POST["action"] == "submit") { ... }
You should be checking the request method. If you've set things up cleanly, a POST request at that URL will mean a form submit. As you've noticed, you can have attempted submits where a value just isn't there.
if ($_SERVER['REQUEST_METHOD'] === 'POST')
See $_POST vs. $_SERVER['REQUEST_METHOD'] == 'POST' for more discussion.
Give your form a name and check for isset($_POST['form_name']) or check for the name of the radio isset($_POST['announcefiles'])
Also, you don't need all the quote escaping that you have, you can use single quotes as well as use a multiline string - see example below.
echo "
<form method='post' name='form_name' action='settings.php' onchange='this.form.submit()'>
Announce New Files: <input type='radio' name='announcefiles' value='On' $checkon1> On
<input type='radio' name='announcefiles' value='Off' $checkoff1> Off<br>
</form>";
<?php
// Check if form was submitted
if (isset($_POST['form_name']) {
// Form submitted
}
?>
<?php
// Check if radio was selected
if (isset($_POST['announcefiles']) {
// Form submitted
echo 'You chose' . $_POST['announcefiles'];
}
?>
Try this:
You may have an easier time if you separate the php and html a little more.
<form method="post" action="settings.php" onchange="this.form.submit()">
<fieldset>
<legend>Announce New Files:</legend>
<label for="on"><input type="radio" id="on" name="announcefiles" value="On" <?php echo $checkon1 ?> /> On</label>
<label for="off"><input type="radio" id="off" name="announcefiles" value="Off" <?php echo $checkoff1 ?> /> Off</label>
</fieldset>
</form>
Then in your php logic in settings.php ( or above the form if you are posting back to the same page ) you can check for the value of announcefiles:
<?php
if(isset($_POST['announcefiles'])){
// DO SOMETHING
}
?>
Let me know if this helps. Or if I'm missing the question.

how to get value of checked with checkbox row in php?

I am trying to get values of rows which are checked using checkbox. So when I submit the form I want values of selected rows but I am not able to figure it out how to get it. Can anybody help me or guide me?
<?php
if(isset($_POST['invite'])){
// what should I write here to get value of hidden fields name and email which was selected
}
?>
<!doctype>
<html><head>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
// performing select all /deselect all checkbox operation
<SCRIPT language="javascript">
$(function(){
// add multiple select / deselect functionality
$("#selectall").click(function () {
$('.case').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){
if($(".case").length == $(".case:checked").length) {
$("#selectall").attr("checked", "checked");
} else {
$("#selectall").removeAttr("checked");
}
});
});
</SCRIPT>
</head><body>
<div class = 'middle' style = 'width : 700px;background-color:beige'>
// creating grid view
<form type="post" name ="contacts">
<span><input type="checkbox" id="selectall"/></span>
<div class = 'middle row'>NAME</div>
<div class = 'middle row'>E-MAIL</div>
<div class='line'></div>
<div class='line'></div>
<?php foreach($email as $e){
echo "<div>
<span><input type='checkbox' class='case' name='case'/></span>
<div class='row'>".$e['name']."
<input name='cname[]' type='hidden' value =".$e['name']."></div>
<div class='row'>".$e['email']."
<input name='cemail[]' type='hidden' value =".$e['email']."></div>
</div><div class='line'></div>";
}
echo"<input type='submit' name='invite' value='invite'>
</form></div></body></html>";
}?>
After submitting the form, the value of a checked checkbox appears in the $_POST array like any other form fields. In your case, you have multiple checkboxes named case. None of those has a specific value assigned, so $_POST['case'] will equal on if any checkbox is checked, so this won't help you at all.
Change the name of the checkbox to case[] and assign a unique value to each, and $_POST['case'] will contain an array of the values of all checked checkboxes after submit. For instance, you could repeat the values of the hidden fields in the value attribute of your checkbox.
I wrote a small php script that should help you to understand, how checkboxes work with php. Just run it, play with the checkboxes and have a look at the $_POST array afterwards.
<!doctype html>
<html>
<body>
<form method="post">
<input type="checkbox" name="test[]" value="0"/>
<input type="checkbox" name="test[]" value="1"/>
<input type="checkbox" name="test[]" value="2"/>
<input type="checkbox" name="test[]" value="3"/>
<input type="checkbox" name="test[]" value="4"/>
<input type="submit"/>
</form>
<pre><?php var_dump($_POST); ?></pre>
</body>
</html>
If you want to keep your hidden fields and don't want to repeat their values in the checkbox element, you can also use the array key of their arrays as checkbox value:
<?php
$i = 0;
foreach($email as $e) {
echo "<div>
<span><input type='checkbox' class='case' name='case[]' value='".$i."'/></span>
<div class='row'>".$e['name']."
<input name='cname['".$i."']' type='hidden' value =".$e['name']."></div>
<div class='row'>".$e['email']."
<input name='cemail['".$i."']' type='hidden' value =".$e['email']."></div>
</div><div class='line'></div>";
$i++;
}
Now, if $_POST['case']contains the values 2, 4 and 8 for instance, the emails you are looking for, are in $_POST['cemail'][2], $_POST['cemail'][4] and $_POST['cemail'][8]. Same applies for $_POST['cname'], of course.
First of all, there's a mistake in an argument of form tag:
<form type="post"
This should read
<form method="post"
Then, since you store name and e-mail in the inputs named cname[] and cemail[], they will be passed respectively in $_POST['cname'] and $_POST['cemail'] variables. Note that you named them in a way they will be passed as arrays, so actually, $_POST['cname'] is an array - it contains a key-value pair of 0 and the value attribute of the input control. So actually, your name and e-mail will be in $_POST['cname'][0] and $_POST['cemail'][0] variables.
Please analyse and also read Dealing with Forms section of PHP manual.

PHP $_POST Alert

I have this form which allows the input of any product quantity from 1-10:
<form method='post' action='cart.php'>
<input type='number' name='quantitychange' size='2' min='1' max='10' value=".$_SESSION["itemsSelected"][$i][1].">
<input type='hidden' name='ProductID' value=".$_SESSION["itemsSelected"][$i][0].">
<input type='submit' value='Update'>
</form>
And another form (button) to display a selection of payment modes:
<form action='cart.php' method='post'>
<input type='hidden' name='next'>
<input type='submit' value='Select Payment Mode'>
</form>
What I want to happen is that when a user did not input anything (1st form), ex. null or 0, I want to display an alert box that says 'Product quantity can't be null or 0'.
Here's my code for that:
if (isset($_POST['next'])) {
if ($_POST['quantitychange']==null || $_POST['quantitychange']==0) {
?>
<script type='text/javascript'>
alert('Product quantity can't be null or 0.');
</script>
<?php
}
else {
echo "
//Payment modes here
";
}
}
The error is that even when a user inputs a quantity bet. 1 to 10, it still displays the alert message. Any help? Thank you.
By the way, the input type "number" only works in Google Chrome browser.
Use a small javascript (or jQuery) function to validate the form before posting it. Have this function throw up the alert if your condition isn't met and then return false. If the condition is met, return true, and it gets submitted.
Edited to add since this might get googled, I'll help a bit with code snippet I have used. The below example is jQuery and was used in production for a web application I made for my employees. document.form.doit.submit(); should be the pure javascript way of submitting the form.
<script type="text/javascript">
function subForm() {
// document.form.doit.submit();
if( test condition passes ) {
$('#save_order').submit();
}
}
</script>
<form id="save_order" action="oms_db.php" method="POST">
<input id="doit" type="button"
value="i am a button" onClick="subForm();">
</form>
I think you have some error in your forms. Instead of the below:
<input type='number' name='quantitychange' size='2' min='1' max='10' value=".$_SESSION["itemsSelected"][$i][1].">
<input type='hidden' name='ProductID' value=".$_SESSION["itemsSelected"][$i][0].">
you should be using something like this:
<input type='number' name='quantitychange' size='2' min='1' max='10' value="<?php echo $_SESSION["itemsSelected"][$i][1]; ?>">
<input type='hidden' name='ProductID' value="<?php echo $_SESSION["itemsSelected"][$i][0]; ?>">
The value parameters in the hidden input fields needs to be echoed from PHP. What you have now is like the value is simple strings ".$_SESSION["itemsSelected"][$i][0].".
I suggest you use
if(empty($_POST['quantitychange'])) { echo 'yourerror'; }
As it is far cleaner then your script. (http://php.net/manual/en/function.empty.php)
Update:
Also, you can't use two seperate forms like you do, your browser only posts whats between
<form>
</form>
Using only one will fix your problem.

post an array to webpage

it has error because $_POST['sub1'] can't be accessed
is there any approach or solution to echo the value of $_POST['sub1']? or impossible? no way? even with another arrays?
i had question about my code nobody solved it! then I decide to tell it in simple way.
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
I think I know what is wrong here.
When you submit the form the second time (for sub2) you are no longer posting the value of sub1 along with it, just sub2.
This should fix it:
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='hidden' name='sub1' value='" . htmlentities($_POST['sub1']) . "'>";
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
You're using submit buttons. Only the button that you actually click will have its name/value pair sent to the server. When you click the sub2 button, only sub2=sub2 is sent, so sub1 won't exist in the $_POST array.
followup:
$_POST is created for you by PHP based on what's sent from the browser. The way you've built your form makes it impossible for 'sub1' to exist when you click the 'sub2' button. In other words, you need to use the SAME name= for BOTH buttons, and change the value= as appropriate:
html:
<input type="submit" name="submit" value="sub1" />
<input type="submit" name="submit" value="sub2" />
php:
if (isset($_POST['submit'])) {
echo "You clicked the {$_POST['submit']} button";
}

Categories