Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7084125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:15:46+00:00 2026-05-28T07:15:46+00:00

Let’s say that I have a file (file1.php) with a simple form with the

  • 0

Let’s say that I have a file (file1.php) with a simple form with the action attribute:

echo'
<form action="foo.php" method="post">
Name:  <input type="text" name="username" /><br />
Email: <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Submit me!" />
</form>';
$another_var = $user_id+5; 

Let’s say the foo.php looks something like this:

$sql ... SELECT user_id, username... WHERE username = $_POST['username']; //or so
echo 'We got the user ID. it is in a variable!';
$user_id = $row['user_id'];

As you see, I need the variable $user_id made in foo.php actually to be used in the main file file1.php.
Is there any way to do this? I though that return $user_id would work but I was wrong :-/
Some notes to have into account:

  • in file1.php there are two forms: one to upload a file (example above) and another to save all the data into a database (that’s the reason I need the variable name).

  • the example is just that, an example. I’m not really adding 5 to the variable requested, but I don’t want to copy and paste 100 lines of code to overwhelm everybody.

  • the variable is also refreshed with javascript, So I see it there but I don’t really know how to assign a javascript variable to a php variable (if possible).

THANKS!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:15:47+00:00Added an answer on May 28, 2026 at 7:15 am

    Here’s how I would do it.

    The html:

    <form id="form1" action="foo.php" method="post">
        <!-- form elements -->
    </form>
    
    <form id="form2" action="bar.php" method = "post">
        <input type="hidden" name="filename" value="" />
        <!-- other form elements -->
    </form>
    

    The javascript

    $('#form1').submit(function(){
        var formdata = ''; //add the form data here
        $.ajax({
          url: "foo.php",
          type: "POST",
          data: formdata,
          success : function(filename){
                //php script returns filename
                //we apply this filename as the value for the hidden field in form2
                $('#form2 #filename').val(filename);
          }
        });
    });
    
    $('#form2').submit(function(){
        //another ajax request to submit the second form
       //when you are preparing the data, make sure you include the value of the field 'filename' as well  
       //the field 'filename' will have the actual filename returned by foo.php by this point
    });
    

    The PHP

    foo.php

    //receive file in foo.php
    
    $filename = uniqid(); //i generally use uniqid() to generate unique filenames 
    //do whatever with you file
    //move it to a directory, store file info in a DB etc.
    
    //return the filename to the AJAX request
    echo $filename;
    

    bar.php

    //this script is called when the second form is submitted.
    //here you can access the filename generated by the first form
    
    $filename = $_POST['filename'];
    
    //do your stuff here
    

    use the Jquery Form plugin to upload the file via Ajax

    $(document).ready(function(){
        $('yourform').submit(function(){        //the user has clicked on submit
    
            //do your error checking and form validation here
    
            if (!errors)
            {
                $('yourform').ajaxSubmit(function(data){        //submit the form using the form plugin
                    alert(data);    //here data will be the filename returned by the first PHP script
                });
            }
        });
    });
    

    As you’ll notice, you haven’t specified either the POST data or the URL of the PHP script. ajaxSubmit picks up the POST data from the form automatically and submits it to the URL specified in the action of the form

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have some text as follows: do this, do that, then this,
Let's say I have the string: hello world; some random text; foo; How could
Let's say that I have a set of relations that looks like this: relations
Let's say that I have a binary that I am building, and I include
Let's say that I have classes like this: public class Parent { public int
Let's say I have a simple UserControl with no codebehind: <UserControl xmlns= .... x:Class=TrafficLight.LightControl>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.