Get value inside foreach

#1

Hello, can you all help me to set the loop value using the foreach? I wanted to set the value to all of the radio button but the value is empty.

$golden = $decodedArray['msg'];
$reeArray = array();
foreach($golden as $key=>$kuda){	
	   $reeArray[] = $kuda['shipping_fee']; 
	   echo "<tr>";
	   echo "<td >"."<input type=radio name=radioName value=echo $kuda[shipping_fee]; />"."$kuda[shipping_fee]"."</td>";
       echo "<td>" .$kuda['shipping_name']."</td>";
	   echo "<td>" .$kuda['shipping_fee']."</td>";
	   echo "<td>" .$kuda['shipping_time']."</td>";
	   echo "</tr>";
#2

then you just have to get out of the string context, use the PHP value, and go back into the string context, like you already did with the other variables.

https://www.php.net/manual/en/language.types.string.php

Also validate your HTML

http://validator.w3.org/

and always surround HTML attribute values with quotes.

#3

This isn’t going to work. You might want to look closer at what you are doing here.

#4

Did you mean i have to changed it to this code?

echo "<td>"."<input type=radio name=radioName value=$kuda[shipping_fee]; />"."$kuda[shipping_fee]"."<td>";

#5

something like that; Otherwise your value will be something like,

echo 10;

Though to keep it cleaner I would do this, personally.

echo "<td><input type='radio' name='shipping' value='{$kuda['shipping_fee']}'/>{$kuda['shipping_fee']}<td>";
#6

ok, i have done that but why my output value is still empty when using this code?

echo "<input type=text id=radioValue style=text-align:center;font-size:large; />";

My javascript is below and i have include the jquery link also

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){
        $("input[type='radio']").click(function(){
            var radioValue = $("input[name='radioName']:checked").val();
            if(radioValue){
				$('#radioValue').val($("[type='radio']:checked").val());  
            }
        });
    });
</script>
#7

The code doesn’t make sense in a few ways.

The input type is text, so it won’t have a checked property.

https://jsfiddle.net/3w1sbevL/

#8

Yes, i wanted the output to be that way like showing the shipping fee inside the box. But the the different is i’m using the forech loop to catch each value.

#9

But you’re not. You just don’t understand what you are doing.

  1. The foreach loop builds the html form.
  2. The form is plain html when it gets to the client.
  3. When a user clicks on a radio button, the jquery code is called to place the value of the radio button into the text input of an html input.

So, your first step is to get the html correct for the client side to use. That means it needs all the attributes to do what it needs to do.

#10

You want me to make a HTML form inside the HTML table? Can you show how to add the code. I have tried it too some days ago but did not know what code to use.

#11

You don’t need the form to be in the table, I don’t know why you need a table at all actually. What you do need to do is fix the html that is being built.

#12

i have no idea what code to add and fix. Can you show me?

#13

I’ve already done that, in the link. It shows what you should have to make it work. If that isn’t enough, you are working on something outside of your skill level and need to start from the beginning on how to write proper HTML first.

#14

ok, thank you. I try to make it work

#15

i have succesfully fix the code but the value does not appear when i’m calling this page from another page with Ajax. Its only working when i’m browsing this page only.

#16

What code do you have to get the value from the other page?

#18

simply follow the lead: the radio button needs, for example, an onclick event. the text input needs an id value (<input type="text" id="test" />). JavaScript or JQuery can be used to change the innerhtml of the text input by its id attribute (test in my code block) when the onclick event is triggered.