Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

PHP form submit

I created the attached frontend using the script attached (test.php).
Now i dont know how to adjust my code so that i can call the addPBRPComment() which is used for updating the table row with the attached comment.

The query in the php code returns two rows of values so the front end shows two sections.
If you observe the front end, there is comment for each while loop iteration
so when users enters comment and clicks on submit button
the values should be saved into DB, i just need comment entered and brpCID values to update the DB.

the script i wrote is not working and not sure what is the reason

also i am not sure how to set the id for textarea in while loop (i think it supposed to be unique)
same case with submit button

Thanks,
frontend.PNG
updatePBRPcomment.php
test.php
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

I think I understand what your code is trying to do but not sure if I have it straight.

Your main loop is creating a number of forms - each form has a text area to save a comment. You want to AJAX the saving of the comment back to the server when the SAVE button is pressed.

What I don't understand is why you are doing it this way. What is the purpose of AJAX'ing the result?

The JSON returned from the call is exactly what you sent + the insert ID from the added comment.

If you can explain a bit more what you are trying to do we can then look at the code.
Avatar of shragi

ASKER

"Your main loop is creating a number of forms - each form has a text area to save a comment. You want to AJAX the saving of the comment back to the server when the SAVE button is pressed."

what you understand so far is exactly correct,

Main purpose of the script:
Each form has a text area and when user enters the comment I want to save it to the DB.

purpose of ajaxing the result ?
I thought this is easier for scalability.
I want to expand this to other buttons, i want to add one more check box saying "close the review" and
if user enters comment and selects this check box then i can simple pass one more variable to the function (id of checkbox)

why i am doing it this way ?
I am new to php and reusing existing code - in one word i don't know any other simpler ways.

so all i am trying to achieve is allow users to enter comment and save the result to DB (in this case append to the existing comment)
may be allow them to close the review (this part of code is not in the attached php)

thanks,
I think what I was asking was why AJAX instead of simply posting back to the page that saves to the DB?

Usually with AJAX it is because you want to update the form in some way dynamically without having to refresh the page by going back to the server - but it appears from the data you are sending back that this not the case.
Avatar of shragi

ASKER

The page will not refresh when i call another php page, that php page updatePBRPcomment.php is just to run the query.
I want to update the DB with out refreshing the page.
Avatar of shragi

ASKER

can some one help me fix this code ?
There were a number of issues with your code - the attached file has comments in to indicate the main changes and issues.

You have a bunch of validation errors - you should consider moving away from using tables to a grid / column layout.

Your original markup was incorrect with the form terminating before the table was complete.

Please see attached file for more information.
t738-fixed.php
Avatar of shragi

ASKER

Thanks JulianH.
couple of question -
1) now the text area id is "pbrpComment_{$indx}"
so how does this effect my updatePBRPcomment.php
I mean previously when i know the id i used to get the value by $_POST['pbrpCID']
so how can i get it now.

2) will the function be called for all forms, i mean if there is a different form in the same php page
and if i want to call different php page for that form, how can I do that.

I agree with you it really hard to read my code, so i changed it now.
Also i tried to run with the suggested changes - looks like it went into the function but did not update the DB
so my guess is i am not catching the values properly.

I attached my updated code.

Thanks

Thanks,
test2.php
1) now the text area id is "pbrpComment_{$indx}"
so how does this effect my updatePBRPcomment.php


When a form is posted it is the name that is used not the ID - you will see the name of the textarea was changed to pbrpComment and the AJAX call used form.serialize() to serialize the form variables - which would work on the name.
The ID's are not used in this situation but they must be unique if they are. I put the index to make them unique and in in the event that you needed to access the id's - but I suspect they are not necessary.

I mean previously when i know the id i used to get the value by $_POST['pbrpCID']
so how can i get it now.


You were calling your function with
addPBRPComment(this.pbrpComment.value, this.brpCID.value)

Open in new window

But nowhere were you setting this.brpCID
You had this line
$brpCID = $obj->brpCID;

Open in new window

But you never used $brpCID

In the solution posted I have added the following to each form
<input type="hidden" name="pbrpCID" value="{$brpCID}" />

Open in new window

This ensures that the value is serialized and posted to your server code

will the function be called for all forms, i mean if there is a different form in the same php page
and if i want to call different php page for that form, how can I do that.


The event handler is linked to the submit event - after that all code is relative to the form that was submitted - so yes it will work for all forms. If you are asking if the code will submit all forms at the same time - the answer is no  - only for the form that the save button was clicked - but each from will save if its save button is activated.

Also i tried to run with the suggested changes - looks like it went into the function but did not update the DB
so my guess is i am not catching the values properly.


I cannot test your db functionality - what you need to do is do insert debug statements in your server script to output results and returned values at each stage of the process.

Then use the console window to inspect what comes back from the script - for instance - place a

print_r($_POST);

At the top of your script - to check that the form values are being posted correctly.

In the test.php you posted I noticed you have removed the HEREDOC statements

echo <<< HTML
....
HTML;

You will need those for your code to work.
					while( $obj = sqlsrv_fetch_object( $stmt)) {
						$indx++;
						$pbrpCID = $obj->brpCID;
						$brpSubCheckList = $obj->brpSubCheckList;
						$actionItemStatus = $obj->actionItemStatus;
						$brpComment = $obj->brpComment;
						$owner = $obj->owner;
						$dueDateString = $obj->dueDateString;
// WHY DID YOU REMOVE THE FOLLOWING LINE AND THE ONE BELOW
echo <<< HTML
						?>
						<div class="boxtestimonial">
						<form id="frmcontact_{$indx}">
							<input type="hidden" name="pbrpCID" value="{$brpCID}" />
...
								</tr>						
							</table>
						</form>
						</div>
						<?php 
HTML;
// AND the ABOVE LINE?

Open in new window

Avatar of shragi

ASKER

This time I used your code and the function is not called when the save button is clicked.
but the function is called when the page is refreshed.

I placed an alert in the function
alert("entered to function" );
to know when the control went to function, when i click on save it does nothing but when i hit refresh page i see the alert.

also i updated my updatePBRPcomment.php as below to test the parameters that are sent.
<?php
print_r($_POST);
$pbrpComment = $_POST['pbrpComment'];
$pbrpCID = $_POST['pbrpCID'];
print_r($_POST['pbrpComment']);
print_r($_POST['pbrpCID']);
?>

Open in new window


so the submit button is still not working.
Is it this hard to allow user to add comment and click save which should update the DB with out page refresh.

Thanks
ee.php
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial