Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Need to send data to sql from div which is dynamically generated with javascript

    Here is the code creating a div with data which I need to send:

    Code:
    function showContent() {
    $("contentZxy").innerHTML = "";
    
    var container = document.createElement("div");
    divWrapper.setAttribute("id", "scrollContainer");
    $("divContentZxy").appendChild(divContainer);
    
    var scrollZxy = document.createElement("div");
    $("scrollContainer").appendChild(scrollZxy);
    var preContent = document.createElement("pre");
    preContent.innerHTML = dataStr1 + dataStr2;
    $("scrollContent").appendChild(preContent);
    CreateScroller("scrollContent");
    
    var divZxy = document.createElement("div");
    divZxy.setAttribute("id", "contentDiv");
    $("displayContentZxy").appendChild(divZxy);
    
    var pZxy = document.createElement("p");
    pZxy.innerHTML = "Enter Data Here";
    pZxy.setAttribute("id", "dataZxy");
    divZxy.appendChild(pZxy);
    
    var pZxy = document.createElement("p");
    pZxy.innerHTML = "Your Data:";
    pZxy.setAttribute("id", "yourDataZxy");
    divZxy.appendChild(pZxy);
    
    var theInputData = document.createElement("input");
    theInputData.type = "text";
    divZxy.appendChild(theInputData);
    
    divZxy.hide();
    
    // save data button *****************************
    $("saveButton").innerHTML = "Save Data";
    $("saveButton").observe("click", clickedSaveButton);
    $("dataContent").show();
    }
    
    function clickedSaveButton() {
    if ($("saveButton").innerHTML == "Save Data") {
    $("saveButton").innerHTML = "Send Data To SQL Database";
    $("scrollContainer").hide();
    $("contentDiv").show();
    } else {
    alert("ERROR YOU MISSED SOMETHING, TRY AGAIN");
    showHomePage();
    }
    }
    Additionally, the file is a javascript file. How can I give reference to a PHP file to post the data?
    Would I have to rewrite the page in PHP?

    I am unsure about what to do next. I need the data which is called through the function to be sent to the database.
    An example of working PHP and SQL code which I might use to send the data, might be:

    Code:
    <?php
    			if(isset($_POST['submit'])){
    				
    				$query = "INSERT INTO user_data
    				  (name, email, data1, data2, data3, data4, data5) VALUES ('".$_POST['name']."', '".$_POST['email']."', '".$_POST['data1']."', '".$_POST['data2']."', '".$_POST['data2']."', '".$_POST['data4']."', '".$_POST['data5']."')";
    
    				mysql_query($query);
    			
    			}else{
    		?>
    I need to integrate the javascript function which calls the data, with the SQL code which sends the data to the database.

    I probably want to use POST
    However, I do not want to use SESSION

    Additionally, the file is a javascript file. How can I give reference to a PHP file to post the data?
    Last edited by webaddict; 04-17-2013 at 03:24 AM.

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,389
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    I think we're going to need to see more scripting, especially the function that creates this: displayContentZxy

    But, somehow I think your solution is going to be AJAX based. This example (see various discussions that are posted), pertains to POSTing values to PHP when a div change is clicked.

    http://stackoverflow.com/questions/1...ata-using-ajax

    This may not be what you're doing, but I think it's going to be really close.
    Someone with javascripting/AJAX experience will probably help you more if you can post more of your script.

    In the least, it might steer you in the right direction (I hope).

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Than the answer is simple: you don't. PHP only accepts data provided to it through a request method such as get, post, put, cookie, etc. HTML div's are not a part of a successful field and cannot be retrieved by PHP. Whilst you can scan HTML, you cannot if its generated by the JS in the first place unless you write an ECMA parser.
    So your options are to use ajax to send the data, populate form fields and manually submit them, or generate anchors and click them.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by mlseim View Post
    I think we're going to need to see more scripting, especially the function that creates this: displayContentZxy

    It will probably help you more if you can post more of your script.
    added to first post
    Last edited by webaddict; 04-17-2013 at 03:23 AM.

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post
    Maybe I can do it all with javascript and sql?

    Perhaps I can add the SQL code into the function clickedSaveButton() ? If so then how can I do this?
    Last edited by webaddict; 04-17-2013 at 03:04 AM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    No, SQL is a server side functionality. I don't think that javascript has a built in SQL library to communicate (at least I sure hope it wouldn't; what a security hole that would be). What you can do is use ajax to issue your request to PHP which can communicate with the dbms. Then you can relay the results back and display.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Afraid I can't help you with that. You'll need to check with the client developers for how to assemble the ajax request.
    PHP wise, it'll simply be in either the $_GET or $_POST depending on the method that was used to send.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •