Link to home
Start Free TrialLog in
Avatar of joaocc
joaocc

asked on

Send extra data with ajaxform

I am using the ajaxform plugin, working great, I just need to post a variable, for example:

> var id = 1;

How Can I do that?

My js code:

$(document).ready(function()
{
 
    var options = { 
    beforeSend: function() 
    {
        $("#progress").show();
        //clear everything
        $("#bar").width('0%');
        $("#message").html("");
        $("#percent").html("0%");
    },
    uploadProgress: function(event, position, total, percentComplete) 
    {
        $("#bar").width(percentComplete+'%');
        $("#percent").html(percentComplete+'%');
 
    },
    success: function() 
    {
        $("#bar").width('100%');
        $("#percent").html('100%');
 
    },
    complete: function(response) 
    {
        $("#message").html("<font color='green'>"+response.responseText+"</font>");
    },
    error: function()
    {
        $("#message").html("<font color='red'> ERROR: unable to upload files</font>");
 
    }
 
}; 
 
     $("#myForm").ajaxForm(options);


//need to post an id variable to the php file
 
});

Open in new window


Php File:
<?php

require 'functions.php';
require 'config.php';

$con = connect($config['DB_HOST'], $config['DB_USERNAME'], $config['DB_PASSWORD'],'maindb');
mysql_query("SET NAMES 'utf8'") OR die(mysql_error());


//GET THE ID VARIABLE FROM JS

//$id = $_POST['id'];

//var_dump($id);




if(isset($_FILES["myfile"]))
{
  if ($_FILES["myfile"]["error"] > 0)
  {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
  }
  else
  {

    
    $imagename1 = 'customName.';
    $pathinfo = pathinfo($_FILES["myfile"]["name"]);
    $extension = $pathinfo['extension'];

    move_uploaded_file($_FILES["myfile"]["tmp_name"], "../imgs/" . $imagename1 . $extension);
 
    echo "Uploaded File :".$_FILES["myfile"]["name"];

    $fullimage = $imagename1.$extension;

    echo "<p>".$fullimage."</p>";
    //echo "<p>".$id."</p>";

    

//UPDATE THE DATABASE WITH THE IMAGE NAME UPLOADED

    //$sqlupdate = "update images set image='$fullimage' where id='$id'";
    //mysql_query($sqlupdate);



  }
}
?>

Open in new window


thank you.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

To post to a php file all you need to do is

<script type="text/javascript">
$(function() {
  var id = $('#some-element').val();
   // or id = $('#some-element').data('id');
   // or however you want to get your id

  $.post('yourscript.php',{id: id}, function(response) {
     $("#message").html("<font color='green'>"+response.responseText+"</font>");
   });
});
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joaocc
joaocc

ASKER

Thank you Sir. Perfect :)
You are most welcome - thanks for the points.
Avatar of joaocc

ASKER

Julian, when u have some time please have a look at this related question:

https://www.experts-exchange.com/questions/28212091/ajaxForm-plugin-dynamic-id.html

Thank you