Jump to content

How to add post back using PHP


cyrixware

Recommended Posts

I still have a problem regrading a post back in my page. The page contains different textbox which is needed to fill all of them. Let say i have 10 textbox. When i fill all the 10 textbox it will automatically save. But when i miss one or many of the textbox to fill-up it will say "Please fill the required fields." But still the other textboxes that i fild up the contents still there all you have to do is key-in or field up the other textboxes that you did not comply.  How to add post back in PHP? I know javascript can do this one but i want to use php. Anybody? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/98249-how-to-add-post-back-using-php/
Share on other sites

ok i have this one:

 

<?php
while($r_display = mysql_fetch_array($q_display)){
$j++;
        <? echo"<input name=studAns[$j] type=radio value=A>"?>
        <? echo"<input name=studAns[$j] type=radio value=B>"?>
        <? echo"<input name=studAns[$j] type=radio value=C>"?>
        <? echo"<input name=studAns[$j] type=radio value=D>"?>
}
?>

When i click submit heres the code:

 

<?php
if($_REQUEST['Submit'] == "Submit"){
include("../connDB.php");

$SCode 		= $_REQUEST['SCode'];
$SuID 		= $_REQUEST['SuID'];
$SItems 	= $_REQUEST['Item'];
$SAns 		= $_REQUEST['studAns'];
$SKey 		= $_REQUEST['Key'];

$count      = count($_REQUEST['studAns']);
$varX      	= count($_REQUEST['Key']);
for ($varY 	= $count ; $varY < $varX   ; $varY++)
{ 
	if ((!$_POST[studAns][$varY]) || ($_POST[studAns][$varY] = " "))
	{
	?>
		<script language="JavaScript">
		//win = window.open('topicareaQuiz.php?ID=<?=$session_id;?>&SuID=<? echo"$SuID";?>&SuCourse=<? echo"$SuCourse"; ?>&SCode=<? echo "$SCode";?>','_self');
	    alert("Please select answer in every item.");	
		</script>

	<? exit;
	}
}

$sql 		= "SELECT * FROM studans WHERE SubCode = '$SCode' AND SuID = '$SuID' ";
$q_add 		= mysql_query($sql);
$num 		= mysql_num_rows($q_add);
if( $num != 0 )
{
	?>
	<script language="JavaScript">
		win = window.open('topicareaStudent.php?ID=<?=$session_id;?>&SuID=<?=$SuID;?>&SuCourse=<?=$SuCourse;?>','_self');
		alert("Topic Area = <?=$SCode;?> submitted already!");	
	</script>
	<?
}else{
    	$i      = strtoupper(count($_REQUEST['studAns']));
			for ($x = 0 ; $x < $i   ; $x++)
			{ 
				$item++;

					$sql_add = "INSERT INTO studans ( SuID, 
													  SubCode, 
													  SubItems, 
													  SubKey, 
													  SubAns
													 ) 
													 VALUES 
													 ( '$SuID'," . 
												 		"'" . "$SCode" ."',".
												 		"'" . $_REQUEST['Item'][$x] . "',".
												 		"'" . $_REQUEST['Key'][$x] . "',". 
												 		"'" . $_REQUEST['studAns'][$x] . "')";
					$q_add 	= mysql_query($sql_add);
					if(mysql_affected_rows() == 0)
					{
						echo "<font color=#FF0000 size=2 face=Verdana, Arial, Helvetica, sans-serif><strong>Cannot Save Record at this time try again later...</strong></font>";
					}else
					{
					?>
						<script language="JavaScript">
							win = window.open('topicareaQuiz.do.php?SCode=<? echo "$SCode";?>&SuID=<? echo"$SuID";?>&SuCourse=<?=$SuCourse;?>&tItems=<?=$tItems+1;?>','_self');
							//alert("Processing... Please click OK to submit the exam.");	
						</script>
					<?
					}
			}

	}
}
?>

 

When i miss one of the irems it will not save.. but the previews answer erased. I want to create a post back so that some of the answers key-in by user will not erase just incase they did not fill-up all the required fields. it will simply focus the items where he/she did not key-in.

I always post back to the same page so that I can check the response, then fall back on the form again if all the stuff isn't there.

 

Yep thats corect. But in my case there is no data in the dbase first... all you have to do is key-in all the required fields. I do this one using javasccript like... ex.

 

<script language="JavaScript">

function form_validator(theForm)

{

  if(theForm.studAns[<?=$varY;?>].value == "") {

alert("Please choose your answer in every item!");

theForm.studAns[<?=$varY;?>].focus();

return(false);

}

}

</script>

 

But i do not know how to integrate this one usng php.. lol....

you are saying "post back", I am saying "post back to the same page", not the same.

 

This is my recommendation:

 

If your form is in the file contact.php, you put your submit script at the top of that page, and you check to see if the form was submitted.

 

If it fails, you simply assign the values sent (using $_POST['varname']) and then, display the form again, instead you assign the value of the field to whatever the varname is, that way, you don't lose any data.

 

 

I don't have anything to add as far as code but I did want to point something out to you:  within a half an hour after your last post of useful information (7:44 to 8:08), you replied with two separate posts because the replies weren't coming fast enough?  That's not exactly the best way to get on the good side of the people of whom you're asking for help.

 

Chill out a little.

 

 

I don't have anything to add as far as code but I did want to point something out to you:  within a half an hour after your last post of useful information (7:44 to 8:08), you replied with two separate posts because the replies weren't coming fast enough?  That's not exactly the best way to get on the good side of the people of whom you're asking for help.

 

Chill out a little.

 

 

 

OK. i want "post back to the same page" as what mb81 said.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.