HTML and PHP in one file - php

I'm a PHP newbie trying to sort some basics out. I have a user-form that leads to a mysql select query, which works fine. Every tutorial I have found so far has the standard form tag, ie: action='script.php' method='post'. This obviously opens script.php in a new tab/window though.
If I don't want to display what's fetched from my db on a different webpage I have to put the html and php in one document together. I didn't think this is how you would really want to do it though.
My specific question is when you want to display stuff on the same page do you just put everything in together within one document and let users hit the submit button?

NO you dont put your php scripts on the same page as your html file/s
Try this link for your reference =)

OR you can put 2 different pages that act as 1 by using INCLUDE FUNCTION
script1.php
<form action="script2.php" method="post" name="myform">
...
<input type="submit" name='submit_button' value="Submit" />
<input
</form>
---------------
script2.php
include 'script1.php';
if(isset($_POST['submit_button']
{.......}

Yeah You can put html and php in single document.
With the help of action.But it not the proper way.
In action you should mention this for writing html and php in same page.
<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>

You can use the same page as Action in form and make condition based on your submit button whthere it is pressed or not.
If it is pressed you can make your Code there for connecting db and do operation like select, insert, update or delete.
e.g.
Your file: script.php
<?php
if(isset($_POST['btnsubmit'])) {
// Do your Operation here...
}
?>
<form action="script.php" method="post" name="myform">
...
<input type="submit" name="btnsubmit" value="Submit" />
<input
</form>

What you can do is simply refer the user back to the form, or another page on your server with the header tag. Inside your PHP script you'd add something similar after your query executes correctly
header( 'Location: ' . $_SERVER['HTTP_REFERER'] ); // Refer to the last page user was on...
Or another URI
header( 'Location: http://some.url/' );

If you really want to do this, here is a way:
<?php
if(isset($_POST)){
//do your php work here
}
?>
<html>
<form method='POST'>
//form elements here
<input type='submit'>
</form>
<!-- other html code -->
</html>

It depends on the length of your code, if the code is too much, then the better way is to include some script file to your parent file. using include() functions, and your perfect answer is yes. just put everything in together within one document

Related

PHP submit blank screen - script not running on POST?

I have a script that pulls an XML page and uses a form to update and save the values back. When I click the submit button it works, but then the page loads blank. I just want the page to refresh. There are about 100 different threads on this, and nothing I have tried has worked to resolve the issue. Out of curiosity, I just tried to run the window.location script and nothing else, and this piece actually doesn't work at all.
<?php
//if (isset($_POST['submit'])) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//$ctstatus->nodeValue = $_POST['ctstatusform'];
//htmlentities($xml->save('test.xml'));
echo '<script type="text/javascript">window.location="http://google.ca";</script>';
}
?>
The inner contents of the form don't really matter at this point, I just want it to refresh the page after I hit the submit button.
I previously used isset but from reading it seems like that's obsolete, and my form action="" used to be blank. Either way my XML save works, but nothing to refresh the page. I also tried header and that didn't work either.
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input class="save" name="submit" type="submit" value="Save" />
</form>
Out of curiosity I tried an onClick function with a timer and this does work but it's not ideal at all, especially because the page could technically refresh before the POST is finished writing the file. I'd rather know why the echo doesn't execute.
PHP redirect would most likely be preferable to JavaScript redirect.
Typical structure when posting back to same page:
<?php // cannot be any output before this (space, linefeed, etc)
if(isset($_POST['submit']) {
// do stuff with the submission
header('Location: http://google.ca');
exit;
}
// does your script need to do some other data retrieval or calculation? do it here.
?>
<html>
... snip ...
<form method="post">
... snip ...
<input class="save" name="submit" type="submit" value="Save" />
</form>
Following this simple structure for procedural scripts--
Deal with user input / redirect
Do logic (collect, manipulate data)
Show output (using php only to insert variables and looping)
will help you avoid a lot of heartache and technical debt.
Okay, I have gotten this sorted out. It turns out that the problem was embarrassingly simple, but maybe will assist someone in the future. Along with reordering my code, as Tim suggested. I specified HTML as the DOCTYPE, and that worked to resolve the issue. I no longer need to worry about refreshing the page after submit, because it refreshes as it should automatically. Thank you to everyone who commented.

WordPress Custom Page Submit Form

I'm trying to write a simple script to write some data to mysql then read it. My code is working without a problem alone but I'm trying to use it inside a WordPress page, this is the point problem starts.
I have created a template file for WordPress, and using this template to create the page. Page shows up without a problem but whenever I try to submit the form inside it (my custom php form) it forwards me to index.php .
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<span>Enter Your Code : </span><br/>
<input type="text" name="sha256"><br/>
<p align = right><input type="submit" name="shaSubmit" value="Submit" /></p>
</form>
this is my form (inside custom php), and as you can see it posts the data to itself. At the the start of my custom php code I have
if(isset($_POST['Submit']))
But it doesn't matter, as soon as I click on button, it forward me to domain.com/index.php
Btw, this custom php is on a page with such url domain.com/custompage/
How can make this form work ?
ps. Code above is for reading from mysql.
You're using the following conditional statement if(isset($_POST['Submit'])) along with the submit button's named element name="shaSubmit".
You need to make those match.
Either by changing the name of your submit button to name="Submit"
or by changing your conditional statement to if(isset($_POST['shaSubmit']))
which is why your code is failing because of the conditional statement you've set is relying on a form element named "shaSubmit".
You need to change 2 things.
(1)make action=""
don't need to use action=" echo htmlspecialchars($_SERVER["PHP_SELF"])"
(2)if(isset($_POST['shaSubmit'])){
the code ....
}

if (isset($_GET['editpersonnel'])) not working

I am working on PHP/MySQL project with a modest CMS.
I am working on a page called - index.php?page=personnel - where I am trying to update a personnel in the database.
On that page I have an included form with - action="?editpersonnel" method="post"
Meanwhile, in PHP:
if (isset($_GET['editpersonnel'])) {
... // update personell ...
header('Location: .');
exit();
... idea being that when the form is submitted the script updates the personnel and then redirects back to itself.
Unfortunately, when I submit the form, instead of reloading the desired 'index.php?page=personnel' the script simply ads 'editpersonnel' to index.php so I end up with
index.php?editpersonnel
And by the way, none of the updates happen either...
Actually, it completely disregards everything after - if (isset($_GET['editpersonnel'])) - so I guess that's were the problem is...
Any ideas what might be causing such a behavior?
p.s.
Form is dynamically populated, so this is its page source:
Name:
Email:
Set password:
You need to use $_GET to be able to use something like that. Change your form's method to GET.
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="text" name="editpersonnel" />
<input type="submit" value="submit">
</form>
Something like that should work.
You are passing the form data by POST method and then trying to access them via GET
You have two possible solutions, see which one is more suitable for your case.
Change form method to GET
<form action="?editpersonnel" method="GET">
OR
Change access method in PHP to POST
if (isset($_POST['editpersonnel'])) {
... // update personell ...
header('Location: .');
exit();
}

Form Action file executing on the same page?

I have a form, on its action i have a php file, i want that when i click submit, it should first open the action php (means view in browser), and then start executing the code in it.
form tag is like this
<form action="myScript.php" method="post"></form>
right now what happen is, when i click submit, it stays on the same page and start executing the php file, when it is done then, then it shows the script file page in browser.
you can use jquery POST and do what ever you want:
jQuery.POST
Is the <input type="submit"> tags etc. inside the <form> tags. If not, then the input buttons are pointless, and just return false.
Your code should look like this:
<form action="myScript.php" method="post">
<input type="submit" />
</form>
Does this help?
Kai :-)
You have to execute the PHP in order to generate the page that is shown in the browser as a result of the POST. Even if your PHP is generating the page and then performing a long operation, output buffering and compression will defeat you, so you will need to turn them off. See How to flush output after each `echo` call? for other things you may have to do to get your output to appear immediately.

Page selection based on submit buttons

In native PHP I can include a javascript code to change the action of a form sent in case I need to direct the user to which page he selects to go like this
<form action="change.php" method="post" name="form">
<input type="submit" value="Click to Page1" onclick="form.action='page1.php';return true;"/>
<input type="submit" value="Click to Page2" onclick="form.action='page2.php';return true;"/>
</form>
I would like to do the same in case I must use codeigniter or cakephp. Someone could help me with this problem ?
CodeIgniter is a backend technology. What you're writing is front end. You're pretty much all set; there isn't really much for you to change. You could, theoretically use CI's form helper, but it's unnecessary...personally, I never use it.
Unless you've removed the index.php file, change the form.action from page1.php and page2.php to index.php/mycontroller/myfunction.
The whole form idea though is sort of flawed; you don't really need it. Why not just use:
onclick="window.location.replace('index.php/mycontroller/myfunction');"
Then you can remove the form all together.

Categories