Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been trying to get user input from a Javascript prompt into a PHP function and have run into a lot a walls doing so. I am at a loss trying jQuery's $.post method- as the PHP simply does not want to execute, not sure why.

Anyways, here is a gist of what I am doing at the moment:

1 A project and its data are loaded from a database- this info is displayed in a table.

2 All data in the table is editable via Javascript prompt(), the code I am using for this is below:

<div id="lvl3"><a href="" onclick="popupprompt(1); return false"><?php echo $fetchdata['name']; ?></a></div>

The above work as such: lvl3 is a tag for font styling; blank href to make it act as a link; popupprompt is the prompt function I made, it takes one argument, the 'type' or what is being edited (1 for project name, 2 for project description, ect); return false so the page doesn't reload; php echo to display project data in the table.

3 Once the user clicks the object above- it executes a javascript function called popupprompt taking an argument of 'type', or what project info is being changed. the code for this function is below:

    function popupprompt(type) {
        switch(type)
    {
        case 1:
            var name = prompt("Project Name:", "");

            if (name != null && name != "")
            {
                //Change Project Name
                var getname = name;
                var gettype = type;

                $.post("edit.php", { type: gettype, name: getname });
            } else if (name == "") {
                senderror("Please enter a valid Project Name");
            } else {
                //Prompt canceled
                sendnotification('Canceled my ass!');
            }
            break;
        case 2:
                            //Description?
        case 3:
            //Version?
        case 4:
            //Release?
        default:
            alert("There was an error processing your request.");
            break;
    } }

Th issue I am having in this function is that nothing in edit.php is executed- and i haven't the slightest clue why. Also, i've had to change the brackets around so it shows properly in the code box- so don't mind those.

4 Anyways, now user input is posted to edit.php- which doesn't work, but i'll post it anyways:

    <?php
    $type = $_POST['type'];
    $name = $_POST['name'];

    switch($type) {
case 1:

    dbconnect();

    $urlext = geturlext();
    $authenticated = isauthenticated();

    if ($authenticated == false)
    {
        echo("<script>senderror('Access denied');</script>");
    } else {

        //Escape and trim input
        $input = trim($input);
        $input = mysql_real_escape_string($input);

        $update = "UPDATE 'projects' SET 'name' = '$input' WHERE 'name' = '$urlext'";
        $updatequery = mysql_query($update) or die(mysql_error());

        echo("<script>sendnotification('Project Name updated');</script>");
        }
    break;
    default:
    break;
    }
    ?>

Again had to move some brackets around. But anyways- this function is supposed to update the data in the database- however, it instead does nothing. I've placed alerts in the beginning and they are never called.

Anyways, long story short- if you know what i'm doing wrong please enlighten me, also, if there is a better way to do this- please let me know!

I appreciate all help, Thanks

share|improve this question
Do you see an Ajax request going out? Use a tool like Fiddler or net tab on your browser's debugger. – epascarello Oct 3 '12 at 18:56
put an alert() immediately before the $.post() call. If you don't get the alert at all, then that code path is not being executed at all. – Marc B Oct 3 '12 at 19:02
I have previously added the 'onsuccess' element to the $.post command (i.e. $.post("edit.php", {type: type, name:name}, sendnotification("success")); ) and I did receive my custom notification- however, nothing in edit.php executed. – Arpy Clarkson Oct 3 '12 at 19:05
Side note: If you're updating a database using AJAX (or any form, for that matter) it's very important to protect against CSRF attacks. Even on internal sites. If you're not sure what that is, Google it to learn more. OWASP has good stuff if you're looking for a place to start. – Pete Oct 3 '12 at 19:23

1 Answer

up vote 0 down vote accepted

Figured it out after a good week of setting it aside. Took a long look at it in firebug, rooted out a typo that was causing most of my issues and also a MySQL syntax error.

I don't know why, but my custom error reporting system does not work when called from a file in this manner. It might be another thing i'm overlooking but firebug sure helped.

Thanks to those who tried to help <3

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.