0

Im sure this question has been asked before but i cannot seem to get what i need working right for me. i need this ajax to process my php form, i got that for, at least i think. I wrote some ajax to do this and it WILL at least give a responce but nothing in the database is changed so its not actually doing anything, heres my ajax/javascript to add a little more detail the reason im doing this is because i dont want the page to redirect after you hit the submit button. id just like it to pop up a success window or something.

<script>
                            function no_redirect(){
                              var note_text=$('#note_text').val();
                              var cnotes=$('#cnotes').val();
                              var  formData = "note_text="+note_text+"&cnotes="+cnotes;
                              $.ajax({
                                  url : "addnote.php",
                                  type: "GET",
                                  data : formData,
                                  success: function(data, textStatus, jqXHR)
                                  {
                                      //data - response from server
                                      alert(data);
                                  },
                              });
                            }
                            </script>

and here is my html form

<form action="" method="get">
                            <div class="span12">
                              <h4 style="color:#1577a6;">Notes</h4>

                              <input type="hidden" name="cnotes" id="cnotes" value="<?php echo $tempuser8[$u]; ?>"/>
                              <textarea class="field span12" id="note_text" name="note_text" type="text" rows="9" maxlength="10000"><?php echo $tempuser9[$u];?>

and just in case here is my php file

$con = mysqli_connect($host,$uname,$pass,$database) or die(mysqli_error($con));

$customer_notes                     = NULL;
$row                                = NULL;


$customer_notes                 = $_GET['note_text'];
$row                            = $_GET['cnotes'];


// $sql = "INSERT INTO userprofile
//             (customer_notes)
//             VALUES
//             ('".$customer_notes."')";

$note_sql = "UPDATE users SET customer_notes='".$customer_notes."' WHERE id='".$row."'";
$result = mysqli_query($con, $note_sql);  
if($result){
    // header( "Location: index.php" );
} else{
    // echo("Input data has failed Please go back and resubmit");
}

idk why its not working right but i AM getting some ouput to teh screen in teh form of a little windows that has all my code in it and says responce from localhost at the top, not sure if that helps

any and all guidance is greatly appreciated!

Edited by berserk

2
Contributors
7
Replies
58
Views
2 Years
Discussion Span
Last Post by berserk
1

First thing - DO NOT use GET for DB manipulation - use POST. GET is used for retrieval (SELECT).

The ajax routine is expecting data of some descrption to be returned, e.g. json, plaintext, html etc. This redirect doesn't make much sense to me. If you're redirecting to another page, then you may as well use a traditional form submission?

Try this:

if($result){
    echo "Data was updated";
} else{
    echo "Input data has failed Please go back and resubmit";
}

Remember though that UPDATE will only return true if there was anything to actually change. If you submit data that is identical to the data already in the record, then no update will occur.

I'm a bit unsure as to what you're trying to achieve with this redirect - you want to display the whole index.php page inside the alert message?

Edited by diafol

Votes + Comments
thank you for pointing out the incorrect use of GET, i needed to use POST but didnt know any better
0

thats all partly my fault for not giving more description as to what im working for. The reason behind this is that i need the page to not redirect, and just the data sent to the database, if i do this with that form it will redirect me every time, i did try a quick fix and put a header in the form action file so that when it redirected it would take me back but thats an unpractical solution for what im working on. Is the a way to push the output from the form php file to a prompt or something on the current screen so that the page isnt redirected but im still getting an response? If you need any more details please ask cause i know this is a tricky scenario, this is not a feature im particularly familiar with so its been really troublesome.

0

ok i have sort of made some progress, i read on some help fofums that

data: $("#note_form").serialize()

is a much better way of getting ALL the data from the form so i tried it and after tweaking some other things i get the prompt working correctly after the button is pressed. however i echo out my sql line from my addnote.php page and there is still nothing reaching the page as far as data. Is there something else im doing wrong? also i changed the method from get to post but it didnt seem to fix this issue so its probably something else im not seeing.

here is the full code for reference

<script>
                            function no_redirect(){
                              // var note_text=$('#note_text').val();
                              // var cnotes=$('#cnotes').val();
                              // var  formData = "note_text="+note_text+"&cnotes="+cnotes;
                              $.ajax({
                                  url : "addnote.php",
                                  type: "POST",
                                  // data : formData,
                                  data: $("#note_form").serialize(),
                                  success: function(data, textStatus, jqXHR)
                                  {
                                      //data - response from server
                                      alert(data);
                                  },
                              });
                            }
                            </script>
                            <form action="#######.php" method="POST" id="note_form">
                            <div class="span12">
                              <h4 style="color:#1577a6;">Notes</h4>

                              <input type="hidden" name="cnotes" id="cnotes" value="<?php echo $tempuser8[$u]; ?>"/>
                              <textarea class="field span12" id="note_text" name="note_text" type="text" rows="9" maxlength="10000"><?php echo $tempuser9[$u];?></textarea>
                              <!-- <input id="add_note" type="submit" class="pull-right" name="add_note" value="Add Note"></submit> -->
                              <a href="#" class="btn btn-info pull-right" onclick="no_redirect()">Add Note</a>
                            </div>
                          </form>

here is the php script

$con = mysqli_connect($host,$uname,$pass,$database) or die(mysqli_error($con));

$customer_notes                     = NULL;
$row                                = NULL;


$customer_notes                 = $_POST['note_text'];
$row                            = $_POST['cnotes'];


// $sql = "INSERT INTO userprofile
//             (customer_notes)
//             VALUES
//             ('".$customer_notes."')";

$note_sql = "UPDATE users SET customer_notes='".$customer_notes."' WHERE id='".$row."'";
echo $note_sql;
echo "<br>";
$result = mysqli_query($con, $note_sql);  
if($result){
    // header( "Location: index.php" );
    echo("Data added successfully!");
} else{
    echo("Input data has failed Please go back and resubmit");
}
0

OKAY i have made a huge amount of progress but now im stumped and i dont know whay its not working. I managed to get the form posting data too and from the database but for some reason it only does the last bit of the form and not the other nested forms that are generated by my loop inside the html file. here is what i have so far, it works for the last entry on the table and will post and update the data i put into that form but will not work on all the other forms that i have, i must be missing something small because i feel like im SOOOO close to figuring out why this isnt working since its working correctly for the last entry in the table.

here is the html code i have come up with.

<script>
                            function no_redirect(){
                              // var note_text=$('#note_text').val();
                              // var cnotes=$('#cnotes').val();
                              // var  formData = "note_text="+note_text+"&cnotes="+cnotes;

                              var xhr;
                                if (window.XMLHttpRequest) {
                                    xhr = new XMLHttpRequest();
                                }
                                else if (window.ActiveXObject) {
                                    xhr = new ActiveXObject("Msxml2.XMLHTTP");
                                }
                                else {
                                    throw new Error("Ajax is not supported by this browser");
                                }

                              var datanote = $("#note_form<?php echo $u;?>").serialize(); // gets all data from your form

                              $.ajax({
                                url : "addnote.php",
                                type: "POST",
                                // data : formData,
                                data: datanote,
                                  success: function(data, textStatus, jqXHR)
                                  {
                                  //data - response from server
                                  alert(data);
                                },
                              });

                            }
                            </script>

                            <form action="#######.php" method="POST" id="note_form<?php echo $u;?>">
                            <div class="span12">
                              <h4 style="color:#1577a6;">Notes</h4>

                              <input type="hidden" name="cnotes<?php echo $u;?>" id="cnotes<?php echo $u;?>" value="<?php echo $tempuser8[$u]; ?>"/>
                              <input type="hidden" name="totalrow" id="totalrow" value="<?php echo $rownumber; ?>"/>
                              <textarea class="field span12" id="note_text<?php echo $u;?>" name="note_text<?php echo $u;?>" type="text" rows="9" maxlength="10000"><?php echo $tempuser9[$u];?></textarea>
                              <!-- <input id="add_note" type="submit" class="pull-right" name="add_note" value="Add Note"></submit> -->
                              <a href="#" class="btn btn-info pull-right" onclick="return no_redirect()">Add Note</a>
                              <div id="div1"></div>
                            </div>
                          </form>
                            </div>
                            <?php } ?>

and here is the php code i have come up with.

$con = mysqli_connect($host,$uname,$pass,$database) or die(mysqli_error($con));

$customer_notes                     = NULL;
$row                                = NULL;
$totalrow                           = NULL;

$totalrow                           = $_POST['totalrow'];
//$row                              = $_POST['cnotes'];

$combined_notes[] = NULL;
$combined_row[] = NULL;


$note_sql[] = NULL;
$result[] = NULL;
$note_text_temp[] = NULL;

for ($i=0; $i <= $totalrow; $i++) { 

    $note_text_temp[$i] = @$_POST['note_text'.$i];
    $combined_row[$i] = @$_POST['cnotes'.$i];

// if(isset($_POST['note_text']))
//  {$customer_notes = $_POST['note_text'];}
// if(isset($_POST['cnotes']))
//  {$row = $_POST['cnotes'];}

$note_sql[$i] = "UPDATE users SET customer_notes='".$note_text_temp[$i]."' WHERE id='".$combined_row[$i]."'";
echo $note_sql[$i];
$result[$i] = mysqli_query($con, $note_sql[$i]);  
if($result[$i]){
    // header( "Location: index.php" );
    echo("Data added successfully!");
} else{
    echo("Input data has failed Please go back and resubmit");
}
}

believe me i know the code looks ugly now but im just trying to figure it out for now, ill clean it up after i get this little problem figured out.

Thank you for all he assistence and guidance, its always greatly appreciated!

1

Really sorry b, but that code is maaking my eyes water. Not sure I can cope with it.

Votes + Comments
simply because this comment was hilarious XD
0

XD LMAO i knew id get that kind of reaction. I literally just went on a for loop tangent to see if i could get the individual note entries to work for each user instead of just the last user, but it obviously did not work. I apologize for (how my old college professor would put it) UGRY COHDE!

1

lol with enough time and effort the solution can be found, and while this solution isnt the best it works just fine, though it may be a little hard to read without some commenting since this is a very unique solution to this problem. I am not promoting this solution as the only way this is simply the method i figured out through lots of reading and running through example code.

here is the script inside my html, this is ajax but has been modified to be generated inside a loop since the note system i had in place is for multiple users inside a table.

<script>
                            function no_redirect<?php echo $u;?>(){
                              // var note_text=$('#note_text').val();
                              // var cnotes=$('#cnotes').val();
                              // var  formData = "note_text="+note_text+"&cnotes="+cnotes;
                              var xhr;
                                if (window.XMLHttpRequest) {
                                    xhr = new XMLHttpRequest();
                                }
                                else if (window.ActiveXObject) {
                                    xhr = new ActiveXObject("Msxml2.XMLHTTP");
                                }
                                else {
                                    throw new Error("Ajax is not supported by this browser");
                                }

                              var datanote<?php echo $u;?> = $("#note_form<?php echo $u;?>").serialize(); // gets all data from your form

                              $.ajax({
                                url : "addnote.php",
                                type: "POST",
                                // data : formData,
                                data: datanote<?php echo $u;?>,
                                  success: function(data, textStatus, jqXHR)
                                  {
                                  //data - response from server
                                  alert(data);
                                },
                              });

                            }
                            </script>

below is the php code i used, it works but is definitly a rough solution, use with caution.

$con = mysqli_connect($host,$uname,$pass,$database) or die(mysqli_error($con));

$customer_notes                     = NULL;
$row                                = NULL;
$totalrow                           = NULL;

$totalrow                           = $_POST['totalrow']; // get total row for the number of times the loop below needs to run to match
// the number of rows that have been populated inside the table.

for ($i=0; $i <= $totalrow; $i++) { 

    $customer_notes = @$_POST['note_text'.$i]; // get customer notes for each rows individual text area input
    $row = @$_POST['cnotes'.$i]; // get the id for each individual so the insert into database can work properly.

$note_sql = "UPDATE users SET customer_notes='".$customer_notes."' WHERE id='".$row."'";
$result = mysqli_query($con, $note_sql);  
}
echo("Data added successfully!");
?>

lastly the html i used to pull it all together.

<form action="#######.php" method="POST" id="note_form<?php echo $u;?>" name="note_form<?php echo $u;?>">
                            <div class="span12">
                              <h4 style="color:#1577a6;">Notes</h4>

                              <input type="hidden" name="cnotes<?php echo $u;?>" id="cnotes<?php echo $u;?>" value="<?php echo $tempuser8[$u]; ?>"/>
                              <input type="hidden" name="totalrow" id="totalrow" value="<?php echo $rownumber; ?>"/>
                              <textarea class="field span12" id="note_text<?php echo $u;?>" name="note_text<?php echo $u;?>" type="text" rows="9" maxlength="10000"><?php echo $tempuser9[$u];?></textarea>

                              <a href="#" class="btn btn-info pull-right" onclick="return no_redirect<?php echo $u;?>()">Add Note</a>
                              <div id="div1"></div>
                            </div>
                          </form>

i hope this helps someone else figure out their issue when confronted with this problem, If there are any questions about the above code please feel free to message me and i will answer the best that i can.

HAPPY CODING!

Votes + Comments
Thanks for coming back with it
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.