Page 1 of 1
Undefined index
Posted: Fri Jan 27, 2012 2:54 pm
by digrev01
could you please say what is wrong with this code
<html>
<form action="imagess.php" method='post'>
<p>
replace:<input name="replacethis" type="text" />
</p>
<p>
with: <input name="withthis" type="text" /><br>
<textarea name="text" cols="33" rows="5"></textarea>
<input name="submit" type="submit" value="replace"/>
</p>
</form>
</html>
<?php
$replace=$_POST['replacethis'];
$wthis=$_POST['withthis'];
$text=$_POST['text'];
str_replace($replace,$wthis,$text);
?>
( ! ) Notice: Undefined index: replacethis in C:\wamp\www\folderimg\findandreplace.php on line 21
Call Stack
# Time Memory Function Location
1 0.0003 364416 {main}( ) ..\findandreplace.php:0
( ! ) Notice: Undefined index: withthis in C:\wamp\www\folderimg\findandreplace.php on line 22
Call Stack
# Time Memory Function Location
1 0.0003 364416 {main}( ) ..\findandreplace.php:0
( ! ) Notice: Undefined index: text in C:\wamp\www\folderimg\findandreplace.php on line 23
Call Stack
# Time Memory Function Location
1 0.0003 364416 {main}( ) ..\findandreplace.php:0
Re: Undefined index
Posted: Fri Jan 27, 2012 3:00 pm
by Celauran
You're referencing variables that may not exist.
Re: Undefined index
Posted: Fri Jan 27, 2012 3:05 pm
by digrev01
i dont understand in the html part i have 3 values and i am referencing between php tags
replace:<input name="replacethis" type="text" />
$replace=$_POST['replacethis'];
Re: Undefined index
Posted: Fri Jan 27, 2012 3:08 pm
by Celauran
And if the form hasn't been submitted and $_POST is empty?
Re: Undefined index
Posted: Fri Jan 27, 2012 3:46 pm
by Hitman47
You need to make sure the form has been submitted.
You can do something like this..
Code: Select all
<?
if(isset($_POST['submit']))
{
//do what you need to do!
}
?>
Re: Undefined index
Posted: Sat Jan 28, 2012 9:41 am
by digrev01
hi,this is the final code when i run it i am seeing some html tags like theese below in the thextarea field and the same error what am i suppsosed to do
<br />
<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: newtext in C:\wamp\www\findandRep.php on line <i>14</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>366360</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\findandRep.php' bgcolor='#eeeeec'>..\findandRep.php<b>:</b>0</td></tr>
</table></font>
<html>
<form action="findandRep.php" method='post'>
<p>
replace:<input name="replacethis" type="text" />
</p>
<p>
with: <input name="withthis" type="text" /><br>
<input name="" type="submit" value="replace">
<textarea name="text" cols="33" rows="5">
<?php
echo $newtext;
?>
</textarea>
</p>
</form>
</html>
<?php
$replace=$_POST['replacethis'];
$wthis=$_POST['withthis'];
$text=$_POST['text'];
$newtext=str_replace($replace,$wthis,$text);
if(isset($_POST['submit']))
{
$newtext=str_replace($replace,$wthis,$text);
}
?>
Re: Undefined index
Posted: Sat Jan 28, 2012 9:54 am
by Celauran
Code: Select all
<form action="findandRep.php" method='post'>
<p>
replace:<input name="replacethis" type="text" />
</p>
<p>
with: <input name="withthis" type="text" />
</p>
<input name="" type="submit" value="replace">
<textarea name="text" cols="33" rows="5"><?php echo (isset($newtext)) ? $newtext : ''; ?></textarea>
</form>
<?php
if(!empty($_POST))
{
$newtext = str_replace($_POST['replacethis'], $_POST['withthis'], $_POST['text']);
}
?>
Re: Undefined index
Posted: Sat Jan 28, 2012 10:14 am
by digrev01
hi again ,i just copied and pasted the code you sent me and after submiting all the things disappeared and nothing changed
Re: Undefined index
Posted: Sat Jan 28, 2012 10:26 am
by Celauran
There was a ) missing. It works fine.
Re: Undefined index
Posted: Sat Jan 28, 2012 10:38 am
by digrev01
than you so much