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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Stuck with sending form data to php via Post

    Evening all,(or what ever time zone your in),
    Hoping you can help, I am trying to submit a form within Javascript via the post method.

    Here is what i'm trying to do. I have a google map that i am submitting locations to a DataBase via php. I can get the data and location to submit without a problem, where i get stuck is when i try to submit data with a file (picture) aswell. I just cant seen to get my head around it, and I have spent a day or more researching this with no luck. I have posted the code, so if you can see what I am missing or what i need to do please help.

    Cheers Jorren,
    PHP script

    <?php

    require("connect_db.php");

    $timezone = "Brisbane/Australia";
    date_default_timezone_set($timezone);
    $today = date("Y-m-d H:i");
    $ext = explode('.',$_FILES['attachment']['name']);
    $extension = $ext[1];
    $newname = $ext[0].'_'.$today.'.';
    $full_local_path = '/home/###/###/###/###/###/###/'.$newname.$extension ;
    $result = $newname . $extension;

    // Gets data from URL parameters
    $name = $_POST['name'];
    $address = $_POST['address'];
    $lat = $_POST['lat'];
    $lng = $_POST['lng'];
    $type = $_POST['type'];
    $attachment = $_POST['attachment'];

    // Opens a connection to a MySQL server
    $connection=mysql_connect ("localhost", $username, $password);
    if (!$connection) {
    die('Not connected : ' . mysql_error());
    }

    // Set the active MySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
    die ('Can\'t use db : ' . mysql_error());
    }

    // Insert new row with user data
    $query = sprintf("INSERT INTO markers " .
    " (id, name, address, lat, lng, type, attachment ) " .
    " VALUES (NULL, '%s', '%s', '%s', '%s', '%s', '%s');",
    mysql_real_escape_string($name),
    mysql_real_escape_string($address),
    mysql_real_escape_string($lat),
    mysql_real_escape_string($lng),
    mysql_real_escape_string($type),
    mysql_real_escape_string($result));

    $result = mysql_query($query);

    if (!$result) {
    die('Invalid query: ' . mysql_error());
    }

    ?>

    Javascript code

    var html = "<form action='#' enctype='multipart/form-data' method='post' id='add_data'>"+
    "<table>" +
    "<tr><td>Name:</td> <td><input type='text' name='name' id='name'/> </td> </tr>" +
    "<tr><td>Address:</td> <td><input type='text' name='address' id='address'/></td> </tr>" +
    "<tr><td>Type:</td> <td><select name='type' id='type'>" +
    "<option value='bar' SELECTED>Bar</option>" +
    "<option value='restaurant'>Restaurant</option>" +
    "</select> </td></tr>" +
    "<tr><td>Attachment:</td><td><input type='file' name='attachment' id='attachment'></td></tr>" +
    "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()' /></td></tr>";
    infowindow = new google.maps.InfoWindow({
    content: html
    });
    function saveData() {
    var url = document.getElementById("add_data").submit();
    downloadUrl(url, function(data, responseCode) {
    if (responseCode == 200 && data.length >= 1) {
    infowindow.close();
    location.reload();
    document.getElementById("message").innerHTML = "Location added.";

    }
    });
    }

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,465
    Thanks
    23
    Thanked 634 Times in 633 Posts
    Your JS does not work.
    Your PHP does not work, because I don't have the database or you did not define variables.

    Do you think there is something missing from what you gave us?
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sunfighter, Thanks for the reply, I haven't posted the full code as the only part i am stuck on the JS handling of submitting a form that has a file attached.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Your <form action='#' ... makes no sense!

    That tells the browser to just jump to the top of the page on form submittal, so no actual post occurs!

    Even if the form *is* submitted, the action for a method="post" can *NOT* be an HTML page, so the form can NOT submit to itself.

    Your action *must* be your PHP page.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That will teach me to go to bed instead of staying up until 0230. I did not see that!
    Anyway, still no joy. The html seems to work, but the JS isn't passing the data.


  •  

    Tags for this Thread

    Posting Permissions

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