Only one value from an hidden input is being displayed - php

<?php
foreach($_color_swatch as $_inner_option_id){
preg_match_all('/((#?[A-Za-z0-9]+))/', $_option_vals[$_inner_option_id]['internal_label'], $matches);
if ( count($matches[0]) > 0 ) {
$color_value = $matches[1][count($matches[0])-1];
?>
<li>
<input type="hidden" id="fakecolor" value="<?php echo $color_value;?>"/>
<div onclick="alert(document.getElementById('fakecolor').value);">
<img src="<?php echo $color_value;?>.png" /></div>
</li>
<?php
}
}
?>
This works for displaying the images, using the $color_value but i need to pass the value from hidden input to another javascript function.
And when i click on div it displays only one value no matter how many are inside the foreach.
Can anyone give me a little help? Thanks.
This is the output:
<li>
<input type="hidden" id="fakecolor" value="red"/>
<div onclick="alert(document.getElementById('fakecolor').value);"><img src="red.png"/></div>
</li>
<li>
<input type="hidden" id="fakecolor" value="blue"/>
<div onclick="alert(document.getElementById('fakecolor').value);"><img src="blue.png"/></div>
</li>
<li>
<input type="hidden" id="fakecolor" value="white"/>
<div onclick="alert(document.getElementById('fakecolor').value);"><img src="white.png"/></div>
</li>
<li>
<input type="hidden" id="fakecolor" value="green"/>
<div onclick="alert(document.getElementById('fakecolorx').value);"><img src="green.png"/></div>
</li>
But when i click on each of the divs, it displays only the value of the second, blue.

Try something like this:
<?php
$cont = 0;
foreach($_color_swatch as $_inner_option_id){
preg_match_all('/((#?[A-Za-z0-9]+))/', $_option_vals[$_inner_option_id]['internal_label'], $matches);
if ( count($matches[0]) > 0 ) {
$color_value = $matches[1][count($matches[0])-1];
?>
<li>
<input type="hidden" id="fakecolor<?php echo $cont; ?>" value="<?php echo $color_value;?>"/>
<div onclick="alert(document.getElementById('fakecolor<?php echo $cont; ?>').value);">
<img src="<?php echo $color_value;?>.png" /></div>
</li>
<?php
}
$cont = $cont + 1;
}
?>
In this way every input hidden have a different id, the same whit the onclick function.
Saludos ;)

I dont quite understand what you're trying to do, still:
<input type="hidden" id="fakecolor" value="<?php echo $color_value;?>"/>
<div onclick="alert(document.getElementById('fakevalue').value);">
First line: you're using an unique id while looping so you'll get several elements with the same id and you'll always end up with the first one with document.getElementById.
Second line: aren't you supposed to get the value of the hidden field ? (ie #fakecolor and not #fakevalue as you're getting).

You have multiple IDs on the same page which will cause getElementById to fail. Why don't you loop and construct your JavaScript like so:
<div onclick="alert("<?php echo $color_value; ?>");">
If you ever move from just alert you can have whatever JavaScript function with a string parameter to accept the color value.

Related

Data from a list php

I would you to know how to get data from a list in php.
<h1>Liste livres</h1>
<ul>
<form method="post" action="">
<?php
$liste = $bdd->query("SELECT * FROM livres");
while($l = $liste->fetch()){
?>
<li><input type="hidden" name="isbn" value="<?php echo $l['livre_isbn']; ?>"/>\<?= $l['livre_titre'] ?>\<?= $l['livre_auteur'] ?>\<?= $l['livre_genre'] ?>\<?= $l['dispo'] ?></li>
<button name="empr" type="submit">Emprunter</button>
<?php } ?>
</form>
</ul>
Every time I click on "Emprunter" for any element displayed, he only take the last element of the list in the procedure. How can I make sure it takes the right element?
For information, the element in the list used is $['livre_isbn'].
Put the <form> tag in your while loop :-)

PHP foreach loop, need loop to get data from db, but it is also looping my forms

So I have a categories table in my database and each category has a certain amount of steps assigned to it. I want to have all of the categories displayed as tabs titled with the category name so I need this from the database. I then have a form inside the tabs to insert the amount of steps needed for that category, but this form is looping... I need it inside the foreach to get the category id but without the form looping... Hope that makes sense..
Here is my code:
<?php foreach($categories as $category){ ?>
<div id="<?php echo $category->category ?>" class="tab">
<form id="maxSteps" method="POST" name="maxSteps" action="<?php $PHP_SELF; ?>" enctype="multipart/form-data">
<label for="maxSteps">Amount of steps in form: </label><input style="width:50px;" id="maxSteps" type="text" name="maxSteps" />
<input type="hidden" name="catId" value="<?php echo $category->cat_id; ?>" />
<input type="Submit" value="Go" name="maxStepsSubmit" />
</form>
<table id="amountOfStepsForm">
<?php $maxStepsById = $wpdb->get_results( "SELECT * FROM metal_work_max_steps WHERE cat_id = '$category->cat_id'" ); ?>
<?php foreach($maxStepsById as $maxStep){ ?>
<tr><td id="maxStepsRow<?php echo $maxStep->id; ?>"><?php echo "<p>Amount of steps in form is: <b>".$maxStep->steps."</b>" ?></td><td id="editRow<?php echo $maxStep->id; ?>"><a id="<?php echo $maxStep->id; ?>" class='edit'>Edit</a></td></tr>
<input type="hidden" name="catId" value="<?php echo $category->cat_id; ?>" id="catId<?php echo $maxStep->id; ?>" />
<?php } ?>
</table>
</div>
<?php } ?>
Regards
The best thing you can do is to run only one query and retrieve all your data inside an associative array. Then just loop trough this data structure.
The query can be done using a JOIN statement, in order to retrieve the information regarding the max steps for each category.
1- put the Form tag above the for loop
2- <input type="hidden" name="catId" val... need to have dynamic name: name="catId_<?php echo $category->cat_id;?>"
3- the button Submit label GO, and the colse tag of Form need to be out of the for loop (below)
so all the fields will be in 1 form,
and you will have 1 submit button, if that what you need,
and the field names are not the same, so on submit you can read all of them
these are general comments, i think you need to pay attention to.

Show whether dynamic radio button is checked or not

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

Need someone to show me how to modify my code to create multiple checkboxes

I am trying to figure our how to modify this code so that it creates a checkbox for each entry in the meta_key.
It is a meta box that will be used on the menu page.
The meta_key has multiple entries that I want to appear in the check list of the meta box. I would also like for the URL inputbox to contain the same meta value that was checked as well. If someone could point me in the right direction I would really appreciate it.
Here is the code I used to echo a list of the meta values from the key. I need to use it to create a checkbox for each value.
<?php echo esc_attr( get_post_meta($post->ID, 'newtheme_section_id', true) ); ?>
Here is the code I am using to create the meta box.
if ( !class_exists('JMO_Custom_Nav')) {
class JMO_Custom_Nav {
public function add_nav_menu_meta_boxes() {
add_meta_box(
'wl_login_nav_link',
__('WishList Login'),
array( $this, 'nav_menu_link'),
'nav-menus',
'side',
'low'
);
}
public function nav_menu_link() {?>
<div id="posttype-wl-login" class="posttypediv">
<div id="tabs-panel-wishlist-login" class="tabs-panel tabs-panel-active">
<ul id ="wishlist-login-checklist" class="categorychecklist form-no-clear">
<li>
<label class="menu-item-title">
<input type="checkbox" class="menu-item-checkbox" name="menu-item[-1][menu-item-object-id]" value="-1"> This is the code for checkbox
</label>
<input type="hidden" class="menu-item-type" name="menu-item[-1][menu-item-type]" value="custom">
<input type="hidden" class="menu-item-title" name="menu-item[-1][menu-item-title]" value="Login">
<input type="hidden" class="menu-item-url" name="menu-item[-1][menu-item-url]" value="<?php echo esc_attr( get_post_meta($post->ID, 'newtheme_section_id', true) ); ?>
<input type="hidden" class="menu-item-classes" name="menu-item[-1][menu-item-classes]" value="wl-login-pop">
</li>
</ul>
</div>
<p class="button-controls">
<span class="list-controls">
Select All
</span>
<span class="add-to-menu">
<input type="submit" class="button-secondary submit-add-to-menu right" value="Add to Menu" name="add-post-type-menu-item" id="submit-posttype-wl-login">
<span class="spinner"></span>
</span>
</p>
</div>
<?php }
}
}
$custom_nav = new JMO_Custom_Nav;
add_action('admin_init', array($custom_nav, 'add_nav_menu_meta_boxes'))
You are telling get_post_meta to bring a single value with the third parameter, try with something like:
$sections = get_post_meta($post->ID, 'newtheme_section_id', false);
foreach( $sections as $section ) {
// do something with $section
}

How to get ID from another file in php?

I'm going to make edit menu in my web. so I direct the page from product into edit page. What I'm confused is how to get the productID from product's page to use in edit page?
Here is my code in product
<?php $query= "SELECT * FROM game";
$rs = mysql_query($query);
while($data = mysql_fetch_array($rs)) { ?>
<div class="gameBox">
<div style="margin:5px;">
<?php echo "<image src=\"images/".$data['gameId'].".png\" alt=\"gameImage\" </image>"?>
<div class="cleaner"></div>
<div class="myLabel">Name</div><div>: <?php echo $data['gameName'];?></div>
<div class="myLabel">Developer</div><div>: <?php echo $data['gameDeveloper']; ?></div>
<div class="myLabel">Price</div><div>: $ <?php echo $data['gamePrice']; ?></div>
<br />
<a href="edit.php" <?php $id=$data['gameId'];?>><input type="button" value="Edit"/></a>
<input type="button" value="Delete"/>
</div>
</div>
<?php } ?>
and it's my code in edit page
<?php include("connect.php");
$id[0] = $_REQUEST['id'];
$query = "SELECT * FROM game WHERE gameId=".$id."";
$rs = mysql_query($query);
while($data = mysql_fetch_array($rs)) { ?>
<form action="doUpdate.php" method="post">
<?php echo "<image src=\"images/".$id.".png\" alt=\"gameImage\" </image>"?>
<div class="cleaner"></div>
<div class="myLabel">Name</div><div>: <input type="text" value="<?php echo $data['gameName'];?>" id="gameName" name="gameName"/></div>
<div class="myLabel">Developer</div><div>: <input type="text" value="<?php echo $data['gameDeveloper'];?>" id="gameDeveloper" name="gameDeveloper"/></div>
<div class="myLabel">Price</div><div>: <input type="text" value="<?php echo $data['gamePrice'];?>" id="gamePrice" name="gamePrice"/></div>
<br/>
<div id="txtError">
<!--error message here-->
</div>
<input type="submit" value="Submit"/>
<input type="button" value="Cancel"/></span>
<?php } ?>
When I try to access edit page, there's an error it said
"Undefined index:$id[0] = $_REQUEST['id'];"
in edit page.
Could anyone help me?
It looks like you're confusing two methods of passing data between pages, forms and query strings in <a href...>s.
Forms:
Data is in <input>-type elements (or friends) and inside a <form...> tag.
For example
<form action="handler.php">
<input type="text" name="var1" />
<input type="text" name="var2" />
<input type="submit">
</form>
Usually passed via POST and accessed in PHP via $_POST.
For example, the values in the text boxes referenced above would be accessed with something like:
<?php
echo $_POST['var1']; // First text box
echo $_POST['var2']; // Second text box
Links:
Passed as query strings in <a href...>, for example:
Click Me
Usually passed via GET and accessed in PHP via $_GET.
For example, the values in the query string provided above would be accessed with something like
<?php
echo $_GET['var1']; // "foo"
echo $_GET['var2']; // "bar"
So in this case it looks like you're hyperlinking an input button -- which is not the usual way to do things, but you would fix it by changing this:
<a href="edit.php" <?php $id=$data['gameId'];?>><input type="button" value="Edit"/></a>
To, this
<input type="button" value="Edit"/>
And then reference the variable in edit.php as $_GET['id'].
But since you know it's going to be an integer and nothing else, something like:
$id = (int)$_GET['id'];
Is good enough sanitation (at least for that variable).
Lastly, I notice you assign a variable to $id[0] but then reference $id. Assigning a variable to $id[0] is not the same as assigning it to $id, as $id is an array in the former and an integer in the latter. It seems to me that you can just drop the [0] w.r.t. $id in your edit.php
You can pass through the query string
<a href="edit.php?<?php $id=$data['gameId'];?>>
In this case your PHP code will get change to
$id[0] = $_SERVER['QUERY_STRING'];
Add the id as a parameter to your edit url:
<input type="button" value="Edit"/>
also at the top of your edit.php:
$id = $_REQUEST['id'];

Categories