• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

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
0
shragi
Asked:
shragi
  • 5
  • 5
1 Solution
 
Julian HansenCommented:
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.
0
 
shragiAuthor Commented:
"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,
0
 
Julian HansenCommented:
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.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
shragiAuthor Commented:
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.
0
 
shragiAuthor Commented:
can some one help me fix this code ?
0
 
Julian HansenCommented:
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
0
 
shragiAuthor Commented:
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
0
 
Julian HansenCommented:
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

0
 
shragiAuthor Commented:
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
0
 
Julian HansenCommented:
It works for me - I had to make some minor modifications to it so I could make it work without the DB - full source attached.
There was also a funny character on line 31 - which needed to be removed.

You can see a working sample here

http://www.marcorpsa.com/ee/t742.php

The updatePBRPcomment.php is unchanged - the version of the script I am using for the main file is attached.

The changes are minimal and do not interfere with the functionality you are trying to establish.

If you open your inspection window you will see for both forms data being sent to the server and returned.
t742.php
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now