1

I've a problem, i need to get a value of an input (text) when his checkbox is selected.

<form method=GET>

<input type =checkbox name = checkbox[]>
<input type = 'text' name=? >

<input type =checkbox name = checkbox[]>
<input type ='text' name=? >

<input type =checkbox name = checkbox[]>
<input type = 'text' name=? >

<input type = 'submit' name='Submit' >

</form> 

This is what I want excactly

If(checkbox == checked)
{
Echo the entered input text value
}

Thanks inadvance.

4 Answers 4

1

Checkbox and radio type field does not get submit when they are not checked or selected.

So you need to check the get/request object whether checkbox name is there or not in the object.

Like

if(isset($_GET['checkbox'])){

}
Sign up to request clarification or add additional context in comments.

Comments

1

When you check checkbox you can get value of next input box.

$("input:checkbox").change(function(){
    if(this.checked){
       alert($(this).next("input").val());
    }
});

Demo

1 Comment

Thanks for your questions, but my question isn't to get the checkbox value, or when it checked, I use there a foreach loop. I want to get the next input text value of the checked checkbox with PHP
0

I am assuming you meant after submission to a PHP script.
I just threw this together and have not tested it yet, but looks fairly straight forward.

It will also work with <input type=checkbox name="c[]">, but just a little apprehensive about the text and checkbox synchronization with the array.

HTML

<form action="./chkbx.php" method=GET>
<input type=checkbox name="c1" value="1"/>
<input type="text" name="t1" />
<input type=checkbox name="c2" value="2"/>
<input type="text" name="t2" />
<input type=checkbox name="c3" value="3"/>
<input type="text" name="t3" >
<input type="submit" name='Submit' />
</form>

PHP

foreach ($_GET as $key => $val){
  $chk[substr($key,0,1)] = intval(substr($key,1,1));
  $txt[substr($key,0,1)][intval(substr($key,1,1))] = $val;
}
echo '<h2>Text=' .  $txt['t'][$chk['c']] . '<h2>'; 

Test Code

<?php
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Menu Test</title><meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style type="text/css">
</style></head><body>

<form action="./chkbx.php" method=GET>
<input type=checkbox name="c1" value="1"/>
<input type="text" name="t1" />
<input type=checkbox name="c2" value="2"/>
<input type="text" name="t2" />
<input type=checkbox name="c3" value="3"/>
<input type="text" name="t3" >
<input type="submit" name='Submit' />
</form>
EOT;
foreach ($_GET as $key => $val){
  $chk[substr($key,0,1)] = intval(substr($key,1,1));
  $txt[substr($key,0,1)][intval(substr($key,1,1))] = $val;
}
echo '<h2>' .  $txt['t'][$chk['c']] . '<h2>';
echo '<pre>';
var_export($chk);
echo"-------------------\n";
var_export($txt);
echo '</pre></body></html>';

Results:

With checkbox c2 checked and text boxes containing "Text One" "Text Two" "Text Three"

Test Page

Below output from is the echo '<h2>Text=' . $txt['t'][$chk['c']] . '<h2>';

Text Two

$chk (
  't' => 3,
  'c' => 2,
  'S' => 0,
)-------------------
$txt (
  't' => 
  array (
    1 => 'Text One',
    2 => 'Text Two',
    3 => 'Text Three',
  ),
  'c' => 
  array (
    2 => '2',
  ),
  'S' => 
  array (
    0 => 'Submit',
  ),
)

Summary:

The submitted check box value is stored in $chk['c']
The submitted Text is in $txt['t'][0], $txt['t'][2], $txt['t'][3] respectfully.

So the text can be retrieved by $txt['t'][$chk['c']

Loop and echo executed in 0.000054 seconds. 54 micro seconds, not too bad.

3 Comments

But I get the checkbox values from the database? I can't use n1, n2
It will put the text value into $text. If checkbox "c1" is checked $text will have the value of text input "t1". I'll post the results of my testing.
Is that what you are looking for?
0

The value of checkbox only returns boolean (i.e. true of false). You should use

if(isset($_GET["checkbox"]))

instead of

if(checkbox == checked)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.