Jump to content

Form submission to DIV


JStuedle

Recommended Posts

I'm sure this has been discussed many many times, but I'm stuck with no idea what to do. I've looked at using jquery to put the results of my php function into a div with no luck - been staring at it for two days now and been through countless forums. 

 


<form method="post" class="ui-buttonset" id="searchform" action="../results">
<div id="radio" class="ui-buttonset">
<input type="radio" id="series1" name="cat1" value="6"/><label for="series1">Category 6</label>
<input type="radio" id="series2" name="cat1" value="7"/><label for="series2">Category 7</label>
<input type="radio" id="series3" name="cat2" value="8"/><label for="series3">Category 8</label>
<input type="radio" id="series4" name="cat2" value="9"/><label for="series4">Category 9</label>
<input type="submit" id="submit" name="submit" value="Show Me My Articles" />
</div>
</form>

The form works perfectly, it takes me back to my results page, and displays the results of the form via my PHP function:


 
 

function results(){
        echo "Radio ".$_POST["cat1"]; echo " Radio2 ".$_POST["cat2"];
}
if(isset($_POST['submit']))
{
results();
}

The problem is - on my results page, the text "Radio # and Radio2 # aren't displayed in the right place, they're in the upper left of the page - outside of every containing DIV. I've been through many forums on using Ajax to get it to place the output into a div, but I can't seem to get the php function to display in the right DIV. Everything besides the display is working as I need it to.

 

Link to comment
Share on other sites

This is an easy one my friend ..... You are echoing out the string in the function. Save it to a variable, return the variable to an echo. ... easier written than explained I guess ... here you go.

function results(){
        $resultsOfForm = "Radio ".$_POST["cat1"]; echo " Radio2 ".$_POST["cat2"];
        return $resultsOfForm;
}
if(isset($_POST['submit']))
{
   echo results(); //or you can save the results
   $resultsVar = results();
   echo $results; // and you can echo the results where ever you want to
}

Hope this helps ....

Link to comment
Share on other sites

Maybe I need to explain a bit better - The results I need are put on the page, just not inside the correct DIV, in fact - they're not in any div at all, they're right after the <body> statement and before the container,header,and content divs. How do I echo the results back into the "content" div rather than right after the <body> statement?

Link to comment
Share on other sites

When you echo out in a function, the function is running before any of the other content on the page is displayed. Try what I suggested. add the return to your function and then put the echo results(); inside the div you want it in. I get that it is at the top of the page, I do it on purpose all the time for error checking my code while I work. You should never, echo out in a function. Bad code form in my books. Return the variable to where it was called, and either echo it out right away or store it in another variable like I described in my first post. It will work. Have a little faith.lol.

 

Adam

Link to comment
Share on other sites

When you echo out in a function, the function is running before any of the other content on the page is displayed. Try what I suggested. add the return to your function and then put the echo results(); inside the div you want it in. I get that it is at the top of the page, I do it on purpose all the time for error checking my code while I work. You should never, echo out in a function. Bad code form in my books. Return the variable to where it was called, and either echo it out right away or store it in another variable like I described in my first post. It will work. Have a little faith.lol.

 

Adam

 

Tried adding the return and echoing the results() function to my content DIV, it still dumps everything at the top of the page. I have faith lol, just didn't understand what you were saying at first - still a little confused actually. This is my first dealings with PHP...bear with me please :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.