Question: PHP Programming Problem !!? I get this error in this code:\n\nError:\nNotice: Undefined index: do in C:&#92;Documents and Settings&#92;me&#92;Desktop&#92;php examp&#92;php38.tmp on line 4\n\nCode:\n<html>\n<body>\n<?\n$do=$_GET['do'];\nif ($do == 'send') {\n$name= $_POST['name'];\n$comments= $_POST['comments'];\n$check= $_POST['check'];\n$which= $_POST['which'];\n$what= $_POST['what'];\n} else { ?>\n<form method="POST" action="<?=$_SERVER['PHP_SELF']?>?do=send">\n<p>Name:<input type="text" name="name" size="20">\n<p>Comments:\n<p><textarea rows="2" name="comments" cols="20"></textarea>\n<p><input type="checkbox" name="check" value="ON"> check this\n<p><input type="radio" value="1" checked name="which"> 1\n<input type="radio" name="which" value="2"> 2\n<p><select size="1" name="what">\n<option>I want this</option>\n<option>no, this is better</option>\n<option>both of them</option>\n</select>\n<p><input type="submit" value="Submit" name="submit">\n<input type="hidden" name="ding" value="ok">\n</form>\n<? }\n?>\n</body>\n</html>

Answer: Remember when you FIRST enter the page, there is no such thing about the variable $do since $_POST array does not exist!\n\nI see multiple problems over there.\n- Your form has the method of POST, so why do you use GET  ?\n +  $do=$_GET['do'];  should be $do=$_POST['do'];\n- Now since the variable do does not exist you could do a simple if statement that checks that. The function isset checks if that variable exists.\n + $do = isset($_POST['do']) ? 'send'  : 'ok';\n- But there is another problem, The hidden name for do does not exist.. So lets change it to do\n + <input type="hidden" name="do" value="ok">\n\n[now the overall code]\n=============================\n<html>\n<body>\n<?php\n\n// Lets initialize the variable\n$do = isset($_POST['do']) ? 'send'  : 'ok';\n\n// Lets just display the current Action\necho 'ACTION:'.$do;\n\n// Check if the hidden variable was sent\nif ($do == 'send') {\n\n // Lets print out the post array\n var_dump($_POST);\n\n} else { ?>\n <form method="POST" action="">\n <p><input type="submit" value="Submit" name="submit"></p>\n <input type="hidden" name="do" value="ok">\n </form>\n<?php } ?>\n</body>\n</html>\n=============================\nI took out the other forms...\n\nThe above should work.. Now.. But why introduce another variable if you already have the submit variable intact? What you could do is say $do = isset($_POST['submit']) ? 'send'  : 'Submit'; That will remove that hidden field. Another thing I realize is that your mixing HTML code with PHP code which is BAD PRACTICE. I recommend you to at least separate them, or if you don't want to use a templating engine, I recommend you do at least store the output in one variable like outvar = "". That will take out alot of issues in the future.\n\nAnyways.. Good Luck
This question from the Yahoo Q&A forum is categorized in the topic: Politics & Government