Get value inside foreach

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

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.

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

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

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

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>

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/

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.

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.

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.

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.

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

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.

ok, thank you. I try to make it work

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.

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

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.

Sponsor our Newsletter | Privacy Policy | Terms of Service