php - function to check if user text is valid -
i trying make function if user clicks update button , text in textbox valid text(only text array should valid ie. text1, text2, text3). then, echo's number assosiated text in array if text1 entered, should echo 10. made function says invalid argument supplied foreach() on foreach loop line.
html:
<input type='text' id='usertext' name='usertext' size='15' /> <input type='submit' name='update' id='update' value='update' />
php:
public currenttext = 0; $config['text'] = array( 10 => 'text1', 25 => 'text2', 50 => 'text3' ); public function set_text($validtext) { foreach($this->config['text'] $key => $value) { // <-- foreach loop if($key == $validtext){ $this->currenttext = $value; } } } if ($_post['update') { $this->set_text($_post['usertext')); }
you may try this:
if ( isset($_post['update']) ) { $this->set_text( $_post['usertext'] ); }
also flip array
;
$config['text'] = array( 'text1' => 10, 'text2' => 25, 'text3' => 50 );
finally change set_text
method this:
// $validtext text1 or text2 or text3 user public function set_text($validtext) { if(array_key_exists($validtext, $this->config['text'])) { $this->currenttext = $this->config['text'][$validtext]; } }
integrate these in class properly, it'll work. check array_key_exists on php
manual.
Comments
Post a Comment