Undefined index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
digrev01
Forum Newbie
Posts: 22
Joined: Fri Oct 07, 2011 9:15 am

Undefined index

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined index

Post by Celauran »

You're referencing variables that may not exist.
digrev01
Forum Newbie
Posts: 22
Joined: Fri Oct 07, 2011 9:15 am

Re: Undefined index

Post 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'];
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined index

Post by Celauran »

And if the form hasn't been submitted and $_POST is empty?
Hitman47
Forum Newbie
Posts: 6
Joined: Thu Jan 26, 2012 5:58 pm

Re: Undefined index

Post 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!
}
?>
digrev01
Forum Newbie
Posts: 22
Joined: Fri Oct 07, 2011 9:15 am

Re: Undefined index

Post 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);
}
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined index

Post 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']);
}
?>
Last edited by Celauran on Sat Jan 28, 2012 10:26 am, edited 1 time in total.
digrev01
Forum Newbie
Posts: 22
Joined: Fri Oct 07, 2011 9:15 am

Re: Undefined index

Post by digrev01 »

hi again ,i just copied and pasted the code you sent me and after submiting all the things disappeared and nothing changed
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined index

Post by Celauran »

There was a ) missing. It works fine.
digrev01
Forum Newbie
Posts: 22
Joined: Fri Oct 07, 2011 9:15 am

Re: Undefined index

Post by digrev01 »

than you so much
Post Reply