Having trouble with assigning a PHP echo varible to document.getElementById - javascript

This works: note-($i = 1)
document.getElementById("range1").setAttribute("value", javascript_array[<?php echo $j++; ?>]);</script>
<p><input type="text" name="range<?php echo $i; ?>" id="range<?php echo $i; ?>" />
This does not work:
document.getElementById("range").setAttribute("value", javascript_array[]);
input type="text" name="range" id="range" />
If I replace "range1" with "range" /not working/
I have read allot and tried several options (+, &, move the "" around) to add to that location but will not work. If I put in "range1" works but not "range". I have the PHP echo in other locations and it works: "value",.., <input type.. name=, id="range<?...
I tried this too and didn't work:
var rangex = "range" + ;
alert(rangex);
document.getElementById(rangex).setAttribute("value", javascript_array[<?php echo $j++; ?>]);</script>
<p><input type="text" name="range<?php echo $i; ?>" id="range<?php echo $i; ?>" />
The alert does come up with "range1" but still will not work. Not sure why StackOF is cutting off the "does not work code"?
Thank you for any help!

Related

How to get the value of the image when clickin

How to get the value of the image for us it on $_POST
$url_f = $crawler->filter('#content .galleries_overview .item')->each(function(crawler $node, $i) {
//echo $node->html();
$image = $node->filter('img')->attr('src');
$src = $node->filter('a')->attr('href');
$href = 'https://example.com/'. $src;
?>
<input type="image" name="submit_blue" value="<?php echo $href ?>" src="<?php echo $image ?>">
<?php
});
?>
you may use onclick
<input type="image" onclick="sendme('<?php echo $href ?>')" src="<?php echo $image ?>">
and add this to your page :
<form method="post" id="myform">
<input type="hidden" name="submit_blue" id="submit_blue" value="" >
</form>
<script>
function sendme(x) {
document.getElementById("submit_blue").value = x;
document.getElementById("myform").submit();
}
</script>
that is a javascript solution i use if i do not want to use jquery or something
By the way I don't like using URL as a value

How can I send to Javascript form elements values. Constantly sending one value

<script>
function tikla()
{
//var radio=document.getElementById('radio').value;
var d2=document.getElementById('buton').value;
alert(d2);
}
</script>
<?php
for($i=1;$i<5;$i++)
{?>
<form>
<?php echo $i; ?>
<input type="text" name="txt" value="<?php echo $i; ?>" disabled="disabled" />
<input type="button" id="buton" value="<?php echo $i; ?>" onclick="tikla(this.value)" />
</form>
<?php }?>
In PHP, I want to send button or text values to Javascript but I can send only one value. I can't see other loop values.
Is it really the intention to have multiple forms? There is no reason why not of course but you can accomplish what you need, generally speaking, using a single form.
By using a nodelist you can iterate through and assign event listeners separately to the HTML - inline event handlers are considered old school nowadays. In this example the event handler is assigned to each button and simply alerts the numeric value assigned to the button ( as in your example ) and also the value and type of the text field to show how you might access it if required.
A quick demo
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>JS Func</title>
<style>
fieldset{border:none;margin:1rem auto}
</style>
</head>
<body>
<form>
<?php
for( $i=1; $i<5; $i++ ) {
$rand = rand(1,99);
?>
<fieldset>
<?php echo $i; ?>
<input type='text' name='txt[]' value='<?php echo $i * $rand; ?>' />
<input type='button' value='<?php echo $i; ?>' />
</fieldset>
<?php
}
?>
</form>
<script>
Array.prototype.slice.call( document.querySelectorAll( 'form > fieldset > input[type="button"]' ) ).forEach( function( bttn ){
bttn.addEventListener('click', function(event){
alert( this.value +' '+this.previousElementSibling.value + ' ' +this.previousElementSibling.tagName )
}, false );
})
</script>
</body>
</html>
pass values in function
<script>
function tikla(val)
{
//var d2=document.getElementById('buton').value;
alert(val);
}
</script>
<?php
for($i=1;$i<5;$i++)
{?>
<form>
<?php echo $i; ?>
<input type="text" name="txt" value="<?php echo $i; ?>" disabled="disabled" />
<input type="button" id="buton" value="<?php echo $i; ?>" onclick="tikla(<?php echo $i; ?>)" />
</form>
<?php }?>

Combining two or more submit buttons into one

I have submit buttons for different section of the webpage. The submit button is used to update the forms and database with the text value in the form fields. Currently, each submit button updates the forms (tied to their respective PKEY id, "consideration_no") only in their own sections. I want to update all the sections forms with one button click.
As you can see from the code below, there are 2 submit buttons. I have tried to link two together through IDs but it did not work for me.
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$question = $answer = "";
$question_err = $answer_err = "";
if(isset($_POST["dg_no"]) && !empty($_POST["dg_no"])){
//counter for array
$counter = 0;
// Get hidden input value
$dg_no = $_POST['dg_no'];
$consideration_no = $_REQUEST['consideration_no'];
$answer = $_POST['answer'];
// Check input errors before inserting in database
if(empty($answer_err)){
// Validate address address
$input_answer = trim($_POST["answer"]);
if(empty($input_answer)){
$answer_err = "Please enter an answer.";
} else{
$answer = $input_answer;
$answer1[$counter] = $input_answer;
}
// Prepare an Submit statement
$sql = 'Update "PDPC".consideration SET answer=:answer WHERE consideration_no = :consideration_no';
if($stmt = $pdo->prepare($sql)){
$stmt->bindParam(":answer", $param_answer);
$stmt->bindParam(":consideration_no", $param_consideration_no);
//$stmt->bindParam(":dg_no", $param_dg_no);
//Set Parameter in while loop, hence new set of parameter for every new form is created and executed.
//Could change the counter loop to a dynamic loop with foreach array.
while ($counter<15){
$param_answer = $answer[$counter];
$param_consideration_no = $consideration_no[$counter];
$stmt->execute();
//$param_dg_no = $dg_no;
// Attempt to execute the prepared statement
//debugggggg
/* $message = $consideration_no[$counter];
$message1 = $answer[$counter];
$message2 = 'lol';
echo "<script type='text/javascript'>alert('$message, $message1, $message2 ');</script>"; */
$counter++;
//apparently redirecting can be placed in the loop, and fields will still get changed.
//header("location: home1.php?dm_no=".$_GET["dm_no"]);
header("location: home1.php?dm_no=".$_GET["dm_no"]);
}
}
if($stmt->execute()){
//Records Submitd successfully. Redirect to landing page
header("location: home1.php?dm_no=".$_GET["dm_no"]);
exit();
} else{
echo "Something went wrong. Please try again later.";
}
// Close statement
unset($stmt);
}
// Close connection
unset($pdo);
} else{
/* --- DISPLAY/READ TABLE, SEE SECTIONS AND ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
// Check existence of dg_no parameter before processing further
if(isset($_GET["dg_no"]) && !empty(trim($_GET["dg_no"]))){
// Get URL parameter
$dg_no = trim($_GET["dg_no"]);
// Prepare a select statement
$sql = 'SELECT * FROM "PDPC".consideration WHERE (dg_fkey = :dg_no AND code_no = 1) ORDER BY consideration_no';
if($stmt = $pdo->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bindParam(":dg_no", $param_no);
// Set parameters
//$param_no = $dg_no;
$param_no = trim($_GET["dg_no"]);
// Attempt to execute the prepared statement
if($stmt->execute()){
if($stmt->rowCount() > 0){
SubSection($subsection1_1); //Consent Collection Subsection
while($row = $stmt->fetch()){
// Retrieve individual field value
$consideration_no = $row["consideration_no"];
$question = $row["question"];
$answer = $row["answer"];
$dg_no = $_GET['dg_no'];
//...time to show the questions and answers with the while loop...
?>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
<label><?php echo $question; ?></label>
<input type="text" name="answer[]" class="form-control" value="<?php echo $answer; ?>">
<span class="help-block"><?php echo $answer_err;?></span>
<input type="hidden" name="consideration_no[]" value="<?php echo $consideration_no; ?>"/>
<input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
</div>
<?php
}
//...after the loop, show the Submit and Cancel button, coz we only need 1 set each section.
?>
<input type="Submit" name = "$consideration_no[]" class="btn btn-primary" value="Submit">
Cancel
</form>
</div>
<?php
}
}
else{
echo "Oops! Something went wrong. Please try again later.";
}
}
Section($section2); //Collection section
// Prepare a select statement
$sql = 'SELECT * FROM "PDPC".consideration WHERE (dg_fkey = :dg_no AND code_no = 2) ORDER BY consideration_no';
if($stmt = $pdo->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bindParam(":dg_no", $param_no);
// Set parameters
//$param_no = $dg_no;
$param_no = trim($_GET["dg_no"]);
// Attempt to execute the prepared statement
if($stmt->execute()){
if($stmt->rowCount() > 0){
SubSection($subsection2); //Consent Collection Subsection
while($row = $stmt->fetch()){
// Retrieve individual field value
$consideration_no = $row["consideration_no"];
$question = $row["question"];
$answer = $row["answer"];
$dg_no = $_GET['dg_no'];
//...time to show the questions and answers with the while loop...
?>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
<label><?php echo $question; ?></label>
<input type="text" name="answer[]" class="form-control" value="<?php echo $answer; ?>">
<span class="help-block"><?php echo $answer_err;?></span>
<input type="hidden" name="consideration_no[]" value="<?php echo $consideration_no; ?>"/>
<input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
</div>
<?php
}
//...after the loop, show the Submit and Cancel button, coz we only need 1 set each section.
?>
<input type="Submit" name = "$consideration_no[]" class="btn btn-primary" value="Submit">
Cancel
</form>
</div>
<?php
}
}
else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
unset($stmt);
// Close connection
unset($pdo);
}
else{
// URL doesn't contain dg_no parameter. Redirect to error page
header("location: error.php");
exit();
}
}
I want it to update all the fields, in different sections, with one submit button
your code was bit difficult to read, but from what i understood you are trying to combine two or more form submissions into one. It's quiet simple
<form method="POST" action="save.php">
<input type=text name=name[] />
<input type=text name=name[] />
</form>
by using the [] to identify the input element you can have multiple values with the same name where you can access them from the PHP script as an array.
For example the above example will produce an array as follows
<?php
print_r($_POST['name']); //("name" => Array....
is this clear enough for you? if not drop a comment, i will explain more. As a side note i do recommend you look into using template engine, and also a framework in your coding project.
Here's what I see when i separate the html into a new file. I tried to remove the excess forms but when I open the last collapsible section, it instantly executes a submit action and brings me back to the home page.
<button class="collapsible"><?php echo $section ?></button>
<div class="content">
<button class="collapsible"><?php echo $subsection ?></button>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<?php
//while loop start
?>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
<label><?php echo $question; ?></label>
<input type="text" name="answer[]" class="form-control" value="<?php echo $answer; ?>">
<span class="help-block"><?php echo $answer_err;?></span>
<input type="hidden" name="consideration_no[]" value="<?php echo $consideration_no; ?>"/>
<input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
</div>
<?php
//while loop ends
?>
<input type="Submit" name = "$consideration_no[]" class="btn btn-primary" value="Submit">
Cancel
</form>
</div>
<?php
//while loop start
?>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
<label><?php echo $question; ?></label>
<input type="text" name="answer[]" class="form-control" value="<?php echo $answer; ?>">
<span class="help-block"><?php echo $answer_err;?></span>
<input type="hidden" name="consideration_no[]" value="<?php echo $consideration_no; ?>"/>
<input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
</div>
<?php
//while loop ends
?>
<input type="Submit" name = "$consideration_no[]" class="btn btn-primary" value="Submit">
Cancel
</form>
</div>
<?php

how can I submit the value of an unchecked checkbox

I have a user edit page with a set of user permissions. Each permission is basically a checkbox. If checked the user has the permission if unchecked the user does not have the permission. So if I want to remove a permission I would uncheck the box and vice versa to add a permission.
I got everything to work using a hidden input, but the problem that I am having is that it is submitting both the hidden input and the checkbox value. For example even if I don't make a change and click the update button, I get a message that reads:
Removed access from 1 permission levels
Added access to 1 permission levels
I will show you my code below. Something to keep in mind is that each input calls a different function.
Here is the inputs:
<ul class="list-group permission-summary-rows">
<?php //List of permission levels user is apart of
foreach ($permissionData as $v1) {
if(isset($userPermission[$v1['id']])){
?>
<li class="list-group-item">
<?php echo $v1['name']; ?>
<span class="pull-right">
<input type="hidden" name="removePermission[<?php echo $v1['id'] ?>]" id="removePermission[<?php echo $v1['id'] ?>]" value="<?php echo $v1['id'] ?>" >
<input type="checkbox" checked data-toggle="switch" name="addPermission[<?php echo $v1['id'] ?>]" id="addPermission[<?php echo $v1['id'] ?>]" value="<?php echo $v1['id'] ?>" >
</span>
</li>
<?php
}
}
?>
<?php //List of permission levels user is not apart of
foreach ($permissionData as $v1) {
if(!isset($userPermission[$v1['id']])){
?>
<li class="list-group-item">
<?php echo $v1['name']; ?>
<span class="pull-right">
<input type="checkbox" data-toggle="switch" name="addPermission[<?php echo $v1['id'] ?>]" id="addPermission[<?php echo $v1['id'] ?>]" value="<?php echo $v1['id'] ?>" >
</span>
</li>
<?php
}
}
?>
Here is the PHP:
//Remove permission level
if(!empty($_POST['removePermission'])) {
$remove = $_POST['removePermission'];
if ($deletion_count = removePermission($remove, $userId)) {
$successes[] = lang("ACCOUNT_PERMISSION_REMOVED", array ($deletion_count));
} else {
$errors[] = lang("SQL_ERROR");
}
}
// Add permission level
if(!empty($_POST['addPermission'])) {
$add = $_POST['addPermission'];
if ($addition_count = addPermission($add, $userId)) {
$successes[] = lang("ACCOUNT_PERMISSION_ADDED", array ($addition_count));
} else {
$errors[] = lang("SQL_ERROR");
}
}
Even if I add the hidden input underneath the checkbox input I receive the same message. I know it has something to do with using different functions so can anyone guide me in the right direction? Should I use some JS code to find if it is checked or not?
If you want to use that trick... you must to name the same both arrays!
<input type="hidden" name="Permission[<?php echo $v1['id'] ?>]" id="removePermission[<?php echo $v1['id'] ?>]" value="0" >
<input type="checkbox" <?php echo ($v1['id']=='YES')?'checked':'' ?> data-toggle="switch" name="Permission[<?php echo $v1['id'] ?>]" id="AddPermission[<?php echo $v1['id'] ?>]" value="1" >
Take a look at "$v1['id']=='YES'" and use the correct comparison. Remember, name must be the same and a value of 0 will be disabled and 1 enabled.
Good luck.
Both the hidden input and the checkbox values are submitted because they're of different input types.
You don't need to hardcode checked explicitly. Write a php if loop to check if the user has permission and echo "checked" accordingly.
//EDIT:
This is the code I would use for this problem. Just overwrite all rules for the user in the DB. Only thing you have to know is the amount of rules (represented by the constant).
<html>
<body>
<?PHP
define("NUMBEROFINPUTS",5);
if(isset($_GET["permissions"])){
$dbperms = array();
foreach($_GET["permissions"] as $permission) {
$dbperms[$permission] = 1;
}
//$dbperms: 0->Input is not set; 1-> Input is set
for($i=0;$i<NUMBEROFINPUTS;$i++){
if(isset($dbperms[$i])) {
echo "Input ".$i." is set<br />";
}
else{
echo "Input ".$i." is not set<br />";
}
}
}
else {
echo '<form>';
for($i = 0;$i<NUMBEROFINPUTS;$i++){
echo 'Permission '.$i.': <input type="checkbox" name="permissions[]" value="'.$i.'" checked /><br />';
}
echo '<input type="submit" /></form>';
}
?>
</body>
</html>
I hope I could help.
use this in jQuery
$("input[type=checkbox]:not(:checked)").attr("value");
well for get and do some with all unchecked checkbox you can collect it into array... and send it on submit to server
var aUnchecked = new Array();
$("input[type=checkbox]:not(:checked)").each(fucntoin(){
aUnchecked.push($(this).attr("name"));
}

Why does my javascript not update quantity in Opencart category.tpl

In category.tpl Opencart 1.5.x
I'm trying to add quantity to the add to cart button.
<div class="cart">
<input type="text" name="quantity<?php echo $product['product_id']; ?>" value="1" size="1" />
<input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>', 'document.form.quantity<?php echo $product['product_id']; ?>.value')" class="button" />
</div>
If I just put a number in for the second variable of addToCart like
addToCart('<?php echo $product['product_id']; ?>', 2);
This adds quantity 2 fine. Checking the element name in ff shows me the number is coming back from php correctly "quantity6.value", etc. Apparently, but when I click Add to Cart nothing happens. Can someone point me in the right direction?
Just to make it more succinct I changed the line to:
" onclick="addToCart('', document.getElementById('quantity').value)" class="button" />
But still the button does not appear to do anything on a click. I assume the document.getElementById().. line is not returning the number properly, but I'm not sure of a good way to test it.
Neither does this seem to work:
<div class="cart">
<input type="text" name="quantity<?php echo $product['product_id']; ?>" value="1" size="1" onchange="updateQty(<?php echo $product['product_id']; ?>);" />
<input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>', getQty(<?php echo $product['product_id']; ?>);)" class="button" />
</div>
...
<script type="text/javascript"><!--
function getQty(num) {
getQty = document.getElementById('quantity' & num).value;
}
function updateQty(num) {
var ibox = document.form.quanity[num].value;
quantity[num] = ibox;
}
...
</script>
This seems a little overdone to be honest. You can do this with one function simply by changing the function on the onclick event to addToCartCategory() and changing the name to an ID for the quantity box. Something along the lines of
<div class="cart">
<input type="text" id="quantity<?php echo $product['product_id']; ?>" value="1" size="1" />
<input type="button" value="<?php echo $button_cart; ?>" onclick="addToCartCategory(<?php echo $product['product_id']; ?>)" class="button" />
</div>
...
<script type="text/javascript"><!--
function addToCartCategory(product_id) {
var qty = $('#quantity' + product_id).val();
addToCart(product_id, qty);
}
...
</script>

Categories

Resources