0

I am getting undefined index errors on a form, I have tried to isset the variable, but nothing, I have globalled all variables that I can find, but I am unable to find a solution, I am sure it is just something simple. But everything I have tried gives me a 500 error.. The issue lies around '{$_GET['XID']}' I have tried '{(isset($_GET['XID']))}' and get a 500 error, I have tried '{(!isset($_GET['XID']))}' I also get a 500 error off this..

Where am I going wrong?

Here is a snippet of the code, and this is the error message I get is:

Notice: Undefined index: XID in C:\wamp64\www\messages.php on line 1039 Call Stack: 0.0108 551240 1. {main}() C:\wamp64\www\messages.php:0 0.2587 1146104 2. mail_compose() C:\wamp64\www\messages.php:50 (would be on line 8 in this snippet though I am pretty sure..)

Snippet:

function mail_compose() { global $pl, $connection, $_SESSION, $q_ry, $_GET, $_POST, $cev, $thismess; echo "<center><table width = '75%' border = '0' cellspacing = '0' class = 'forum'> <tr bgcolor = '#999999'><td colspan=2><center><b>Send a message</b></td></tr> <form name = 'reply' method = 'post' action = 'messages.php?action=send'><tr> <td width = '25%' bgcolor = '#E3E3E3'><b>Users ID:</b></td> <td bgcolor=#E3E3E3><input type = 'text' name = 'to' size = '5' maxlength = '10' value = '{$_GET['XID']}'> <i><font size=2>(Example. 1)</font></i></td> </tr><tr> <td width=25% bgcolor=#E3E3E3><b>Subject:</b></td> <td bgcolor=#E3E3E3><input type = 'text' name = 'subject' size = '50' maxlength = '50' value=''> <i><font size=2>(Example. Hello)</font></i></td> </tr><tr><td bgcolor = '#E3E3E3' colspan = '2' align = 'center'> <a onClick=\"addwithEase(' :) ')\"><img src='images/smilies/smile.gif'></a> <a onClick=\"addwithEase(' :P ')\"><img src='images/smilies/tounge.gif'></a> <a onClick=\"addwithEase(' :( ')\"><img src='images/smilies/sad.gif'></a> <a onClick=\"addwithEase(' :o ')\"><img src='images/smilies/shocked.gif'></a> <a onClick=\"addwithEase(' :@ ')\"><img src='images/smilies/angry.gif'></a> <a onClick=\"addwithEase(' o_O ')\"><img src='images/smilies/sarcy.gif'></a> <a onClick=\"addwithEase(' :s ')\"><img src='images/smilies/confused.gif'></a> <a onClick=\"addwithEase(' ;) ')\"><img src='images/smilies/wink.gif'></a> <a onClick=\"addwithEase(' :* ')\"><img src='images/smilies/cool.gif'></a> <a onClick=\"addwithEase(' :|o ')\"><img src='images/smilies/psyc.gif'></a> <a onClick=\"addwithEase(' :| ')\"><img src='images/smilies/dissapointed.gif'></a> <a onClick=\"addwithEase(' :D ')\"><img src='images/smilies/grin.gif'></a> <a onClick=\"addwithEase(' xD ')\"><img src='images/smilies/histericle.gif'></a> <a onClick=\"addwithEase(' :L ')\"><img src='images/smilies/laughing.gif'></a> <a onClick=\"addwithEase(' xP ')\"><img src='images/smilies/histericletounge.gif'></a> </td></tr> <tr> <td bgcolor = '#E3E3E3'><b>Message:</b></td> <td width = '90%' bgcolor = '#E3E3E3'> <textarea name = 'message' rows = '10' cols = '75%'></textarea></td> </tr><tr bgcolor = '#E3E3E3'> <td colspan = '2'><center><input type = 'submit' value = 'Send'></center></td> </tr></form></table></center><script language=\"JavaScript\" type=\"text/javascript\"> function addwithEase(smileToAdd) { document.reply.message.value += smileToAdd;document.reply.message.focus(); } </script> <hr width = '75%'/>&gt; <a href='messages.php'>Back</a><hr width = '75%'/>"; } 
4
  • How is mysqli related? What is the URL you load this page with? Commented Oct 16, 2017 at 16:12
  • Check your apostrophes in your $_GET. If I copy your code in my editor, they are different so value = '{$GET[' is what I get. Also since $_GET is called withing an echo statement, you can do $_GET[XID] directly. Looks like a quote issue: php.net/manual/en/language.types.string.php Commented Oct 16, 2017 at 16:14
  • Thank you i have just got into bed I will try it on the computer in thw morning.. And the URL is reunited-city.com/messages.php?action=new I don't think signup.php is posting anything to the database.. Actually come to think of it not much is really to be honest Commented Oct 16, 2017 at 16:20
  • @bjmag94 The answer below should resolve the question. I thought you were asking why the index was undefined, not how to handle it when not defined. Commented Oct 16, 2017 at 16:40

2 Answers 2

2

You can't put an if statement in the middle of a string literal.

Do your test outside.

$xid = ""; # This is the default if (isset($_GET['XID'])) { $html_safe_xid = htmlspecialchars($_GET['XID']); } echo "<center> etc etc etc $html_safe_xid etc etc etc"; 
Sign up to request clarification or add additional context in comments.

1 Comment

You kinda can with the ternary operator, but judging OP's level, I think it's good that you showed the more verbose version.
0

As far as I understand, the problem is getting the data passed to the get method script. Unfortunately, you did not specify how you pass to the string function, hence I assume that eg? XID = test. If this is the case, you should note the case in the name of the method - if it is '{$ _GET [' XID ']}' and pass the string using xid = test then you will have undefined index: XID. Correct should be? XID = test. To exclude sensitivity in the get method name for uppercase, lowercase letters, add the second isset condition for lowercase method names and get the variable displayed in echo.

enter code here

$xid = ''; if(isset($_GET['XID'])) $xid = $_GET['XID']; elseif(isset($_GET['xid'])) $xid = $_GET['xid']; echo "<td bgcolor=#E3E3E3><input type = 'text' name = 'to' size = '5' maxlength = '10' value = '$xid'>"; 

3 Comments

Thank you, worked a treat, now time to fix all queries
If my answer helped solve the problem please repay the points
Sorry I can't yet as I don't have 15 reputation, otherwise I would, thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.