Jump to content

Can't take value from textbox

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
socheata

socheata

    Newbie

  • Members
  • Pip
  • 3 posts
Anybody can help me?
I can't take value from textbox with array ,
Please Check code below, i am not clear what is the problem ?

Cheers,
Socheata

<?php

	if(isset($_POST["submit"])):	

		$book= array("PHP"=>"Hypertext Preprocessor",

		"ASP"=> "Active Server Page",

		"JSP"=>"Java Server Page",

		"CSS"=>"Cascading Style Sheets");

			$st = false;

			foreach ($book as $index => $value):

			echo "$value <br>";

				if ($st = true): 

					break;

				endif;

			endforeach;

		if($st == false)

		{

		echo "No result found";

		}

	endif;

?>

<form action="nonsequen.php" method="post">

	<input type="text" name="text" > 

	<input type="submit" name="submit" value="submit" />

</form>

Edited by Orjan, 22 September 2010 - 10:44 PM.


#2
Drew

Drew

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
If you are trying to find if the value is in the array and echo it, then I would suggest trying this.
<?php
if(isset($_POST["submit"]))
{
  $text = $_POST['text'];
  $book= array("PHP"=>"PHP Hypertext Preprocessor",
  "ASP"=> "Active Server Page",
  "JSP"=>"Java Server Page",
  "CSS"=>"Cascading Style Sheets");

  $st = false;

  foreach ($book as $index => $value)
  {
    if ($text == $index || $value)
    {
      echo "Result - " . $index . " : " .$value . "<br />";
      $st = true;
    }
  }


  if($st == false)
  {
    echo "No result found<br />";
  }

}
?>
Also ("PHP" => "PHP Hypertext Processor")


If this is not what you were after, please elaborate on what you are querying and I would be more than happy to assist you. But from what you have given, this is what I assumed and have given information for.

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
sounds a lot of unncesary code to me. this will easily do the same.


<?php

if(isset($_POST["submit"]))

{

  $book= array("PHP"=>"PHP Hypertext Preprocessor",

  "ASP"=> "Active Server Page",

  "JSP"=>"Java Server Page",

  "CSS"=>"Cascading Style Sheets");


    if (array_key_exists($_POST['text'], $book) {

        echo "Result - " . $_POST['text'] . " : " .$book[$_POST['text']] . "<br />";

    } else {

        echo "No result found<br />";

    }

}


__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
socheata

socheata

    Newbie

  • Members
  • Pip
  • 3 posts
Thank you !

#5
Drew

Drew

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Well, yes, there was unneccisary code, because ti seemed like it would be used elsewhere as well.

Thanks for tidying it up, looks better to me as well.

Also, mine looks longer because it has more logical formatting, well, as I was taught to do. Not saying yours is wrong or nothing, just saying about the way I was taught to format my code so it's easy to follow.

Mainly bracket wise.

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
about on how to write brackets, it's many different schools on how to do it. best if you choose one and stick to that one, but learn some of the others ways too, so you can make changes in others code and write as they done in that program so your change look different. It wasn't supposed to change that... I just wanted to show that it was possible to do the same in a simple manner, that actually can be easier to follow, and with less variables used to follow, but you are right, in a larger context, it might not be the ideal solution, as every design decision depends on the context.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
Drew

Drew

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
I agree completely with you there wise Orjan.
You did what you did, and it's another way to do it. And there are many ways. "To each their own."
You put that a very good way.