Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Rony Alvarez
Rony Alvarez
14,515 Points

Sending two http requests simultaneously to one php file?

I'm building my first php application, I have two html forms in one php file and I need to send both at the same time. I understand that http only sends one request at a time to the server, is it possible to sent two form requests simultaneously?

Here is my code:

    <head>
        <title> City Comparison </title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>

<body class="wrapper">

    <header class="headContainer">
        <div class="headItem">
            <a href="index.php" id="logo">
                <h1> City Comparison </h1>
            </a>
        </div>

        <div class="headItem">
            <nav>
                <ul>
                    <li><a href="about.html" class="selected"> About </a></li>
                    <li><a href="contact.html"> Contact </a></li>
                </ul>
            </nav>
        </div>
    </header>

    <div class="bodyContainer">
        <div class="item">
            <form action="SubmitProcess.php" method="post"> 
                    <div class="box">
                      <div class="container-1">
                        <span class="icon"><i class="fa fa-search"></i></span>
                        <input type="text" name="city" id="search" list="suggestions" placeholder="Search..." />
                        <datalist id="suggestions">
                            <option value="New York City">
                            <option value="Los Angeles">
                            <option value="Chicago">
                            <option value="Houston">
                            <option value="Philadelphia">
                            <option value="Phoenix">
                            <option value="San Antonio">
                            <option value="San Diego">
                            <option value="Dallas">
                            <option value="San Jose">
                        </datalist>
                      </div>
                    </div>
            </form>
        </div>

        <div class="item">
            <h3> vs </h3>
        </div>

        <div class="item">
            <form action="SubmitProcess.php" method="post"> 
                    <div class="box">
                      <div class="container-1">
                        <span class="icon"><i class="fa fa-search"></i></span>
                        <input type="text" name="city2" id="search" list="suggestions" placeholder="Search..." />
                        <datalist id="suggestions">
                            <option value="New York City">
                            <option value="Los Angeles">
                            <option value="Chicago">
                            <option value="Houston">
                            <option value="Philadelphia">
                            <option value="Phoenix">
                            <option value="San Antonio">
                            <option value="San Diego">
                            <option value="Dallas">
                            <option value="San Jose">
                        </datalist>
                      </div>
                    </div>
            </form>
        </div>
    </div>

</body>

4 Answers

Colin Marshall
Colin Marshall
32,861 Points

You cannot send two forms at once. From reading your code, I see no reason why this all can't be one form. You just need to give unique id's and names to all of your form elements/inputs, like city1 and city2.

I agree with Colin. It should be one form. You can always add blank lines styled with CSS so that it looks like two different forms, but I cannot imagine why you would really want to do that.

Looking more at your code, I think there is much room for improvement. You could have PHP that only has one list of cities and automatically removes the city selected first from the second list. You could also have them as radio buttons so that the cities display clearly with your selections.

Another option would be to have a two column table with the vs. in between. The cities could be listed with radio buttons next to them. If this is sports related, you may want the columns Home and Away.

Put some thought into what you really want, then reformat into one form.

Rony Alvarez
Rony Alvarez
14,515 Points

Thanks for your help! I was able to send the two requests using ajax.

Colin Marshall
Colin Marshall
32,861 Points

Just so you know, even though that works, it is bad practice to handle forms like that. This should definitely be one form. One reason being semantics, another reason being that you are going to double the amount of requests sent to the server.