i am creating an blog system where i can use only static files . I have section where i am creating a new post and i write it on a text file like this:
new_post. php
<form id=" form1" name="form1" method="post" action="writeblog.php">
<p>
<label>TITLE: </label><input type="text" name="title"/>
<label for="body"> Body: </label>
<textarea name="body" cols=50 rows=10></textarea>
<br />
<input type="submit" name="submit" value="POST"/>
</form>
writeblog.php
<?php
$out = fopen("blog.txt","a");
if(!$out){
header('Location: new_post.php');
exit;
}
$title=$_POST[title];
$body=$_POST[body];
$body=str_replace("\n"," ",$body);
fputs($out,"\n");
fputs($out,date("m/j/y h:i \n ",time()));
fputs($out,"$title\n\n\n");
fputs($out,"$body\n\n\n\n\n");
fclose($out);
header('Location: new_post.php');
exit;
?>
I need some help with how I can work with just one post from the file delete it or edit it insert in on page.
Related
I am trying to re-do my site to wordpress, I have this problem. I have a form and on a submit php file runs and adds data to database. How do I run php file in wordpress?
<form method="POST" action="insert.php">
<p>
<label for="XX">XX</label>
<input type="text" name="XX" id="XX">
<label for="X">X</label>
<input type="text" name="X" id="X">
<label for="XXY">XXY</label>
<input type="text" name="XXY" id="XXY">
</p>
<input type="submit" value="add to database">
</form>
How do I run the php insert.php file? Thank you!
Or on the other hand, could I write in action the directory to the file? eg. wp-content/myphpfiles/insertgolf.php, would that work? Thank you, I am new to wordpress
In WordPress form page, you can not put php file path. If you want to do this then you need to create a template.
Please follow reference link, here you can know how to create template in WordPress: https://www.cloudways.com/blog/creating-custom-page-template-in-wordpress/
In template file put your code:
<?php /* Template Name: insertGolf */
if(!empty($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){
//do database related code here
$link = mysqli_connect("c108um.DDD.com","f96860","qt3C2EQ","f96860");
if($link === false){
die("ERROR: " . mysqli_connect_error());
}
$datum = mysqli_real_escape_string($link, $_REQUEST['datum']);
$odkud = mysqli_real_escape_string($link, $_REQUEST['odkud']);
$sql = "INSERT INTO golf (datum, odkud) VALUES ('$datum', '$odkud')";
if(mysqli_query($link, $sql)){ header('Location: golf.php');
exit;
}
else{
echo "ERROR: " . mysqli_error($link);
}
mysqli_close($link);
}
?>
<form method="POST" action="">
<p>
<label for="XX">XX</label>
<input type="text" name="XX" id="XX">
<label for="X">X</label>
<input type="text" name="X" id="X">
<label for="XXY">XXY</label>
<input type="text" name="XXY" id="XXY">
</p>
<input type="submit" value="add to database">
</form>
I'm trying to get user input in a progressive sequence that leads to that input being sent by email. Sending by email is a whole other issue that I haven't tackled yet so not really worried about that.
The part I am having difficulty with is once the user gets to the "Send Email?" (Yes/No) radio buttons, the input from that question is not processed correctly.
I've gotten further with this by using a separate php file as the form action but still get errors related to emailName, emailAddress, and emailMsg not existing ("Notice: Undefined index...").
Furthermore, I still need to be able to use the $_POST[athletes] array further down but I'm guessing it's outside of the variable scope at that point.
So to bring that all together, I'm really asking a few questions:
1) How can I get all of the forms to work together in the same file?
2) When the program actually goes past the "Send Email?" radio buttons when I use a separate php file as the form action, why am I getting undefined index errors?
3) Why do I get an error when I try to use the athletes[] array further down in the code? Should I somehow be passing the array values to that part of the code?
The exact steps the user would take to get to the issue is:
Select 1 or more athlete checkboxes and click the 'Display Selection(s)' button.
Select 'Yes' for "Send Email?" and click the 'Submit' button.
Restarts the code for some reason.
Any help would be greatly appreciated. Also, this is my first post so sorry if I asked the question incorrectly or not according to site etiquette.
I also apologize for the long code fragment but I'm not sure what parts might be causing this to be incorrect.
<b><h1><center>Athelete Selection Screen</center></h1></b>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<fieldset>
<legend>Athletes Available: </legend>
<input type="checkbox" id="student1"
name="athletes[]" value="Student1 Test">
<label for="student1">Student1 Test</label><br/>
<font color="grey">Football - Running back</font><br/>
<p>
<input type="checkbox" id="student2"
name="athletes[]" value="Student2 Test">
<label for="student1">Student2 Test</label><br/>
<font color="grey">Soccer - Left Forward</font><br/>
</p>
<p>
<input type="checkbox" id="student3"
name="athletes[]" value="Student3 Test">
<label for="student1">Student3 Test</label><br/>
<font color="grey">Baseball - Pitcher/Left Outfield</font><br/>
</p>
</fieldset>
<p>
<?php echo("\t\t\t\t\t"); ?><button type="submit" name="submit" value="submit">Display Selection(s)</button>
</p>
</form>
<fieldset>
<legend>Athletes You Selected: </legend>
<?php
if (!empty($_POST['athletes']))
{
echo "<ul>";
foreach($_POST['athletes'] as $value)
{
echo "<li>$value</li>";
}
echo "</ul>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<fieldset>
<legend>Send Email? </legend>
<input type="radio" id="Yes"
name="radioSendMsg[]" value="Yes">
<label for="student1">Yes</label>
<p>
<input type="radio" id="No"
name="radioSendMsg[]" value="No">
<label for="student1">No</label><br/>
</p>
<button type="submit" name="submitRadio" value="submit">Submit</button>
</p>
</form>
<?php
if (!empty($_POST['radioSendMsg']))
{
foreach($_POST['radioSendMsg'] as $radioMsg)
{
if($radioMsg == "Yes")
{
echo "\tPlease enter information regarding the email to be sent: ";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<label for="emailName"> Name: </label><br/>
<input type="text" size="25" id="emailName" name="emailName" />
</p>
<p>
<label for="emailAddress">E-mail Address: </label></br>
<input type="text" size="25" id="emailAddress" name="emailAddress" />
</p>
<p>
<textarea id="emailMsg" name="emailMsg" cols="30" rows="5"></textarea>
</p>
<button type="submit" name="emailSubmit" value="send">Send Message</button>
</form>
<?php
$msg = "Name: ".$_POST['emailName']."\n";
$msg.= "E-Mail: ".$_POST['emailAddress']."\n";
$msg.= "Message: ".$_POST['emailMsg']."\n";
$msg.= "<ul>";
foreach($_POST['athletes'] as $value)
{
$msg.= "<li>$value</li>\n";
}
$msg.= "</ul>";
$emailRecipient = "sjzerbib#gmail.com";
$emailSubject = "Athlete Selection Submission";
$emailHeaders = "From: Sebastien\n"."Reply-To: ".$_POST['emailAddress'];
mail($emailRecipient,$emailSubject,$msg,$emailHeaders);
echo "Message sent: \n".$msg;
}
else
{
?> <p /> <?php
echo "\n\nNo email will be sent for your last athlete selection.";
?>
<br/>Please click here
to return to the Athlete selection screen.
<?php
}
}
}
}
When you submit a form, only those controls contained within that form are included. The exception is successful controls that have the form attribute set to the id value of the form that was submitted.
So, given you had something like:
<form id="form-1" method="post">
<input type="text" name="first-input" />
</form>
<input type="text" name="second-input" />
The only value to be submitted would be that of first-input. If you add the form attribute to second-input:
<input type="text" name="second-input" form="form-1" />
Then the submission of the form would include both values. Unfortunately, the form attribute is not fully supported (IE and Edge have no support).
Of course, your markup is invalid, so that's a moot point. For starters, you cannot nest a form within a form. How a browser responds to markup that violates that rule is up to it's vendor, but in my experience is somewhat unpredictable. You're also using deprecated tags (<font> and <center> are no longer valid) and nesting elements incorrectly (<h1> is a block level element, whereas <b> is inline).
If you're doing a full submit each time (so the page gets submitted to itself and then reloads), then just use some sort of conditional to only render the dependent controls if the preceding form submissions were successful:
<?php
$canDoNextStep = !empty($_POST['input-1']);
?>
<form id="form-1" method="post">
<input type="text" name="first-input" />
<?php if(canDoNextStep): ?>
<input type="text" name="second-input" />
<?php endif; ?>
</form>
Lastly, whitespace is (mostly) ignored when your browser parses and displays your HTML, so you can lose the \t and \n values in your strings, unless you're concerned about how your markup looks if someone chooses to view source when using your form.
I have this issue with my validation and posting the data to another page.
Here is my form:
Signup.php
<form id="regForm" action="<?php echo htmlspecialchars($_SERVER["submit.php"]);?>" method="post" name="regForm">
<label for="fname">First Name:</label><input name="fname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];}?>"/><br/>
<label for="mdname">Middle initial:</label><input name="mdname" type="text" size="10" maxlength="35" value="<?php if(isset($_POST['mdname'])){echo $_POST['mdname'];}?>"/><br/>
<label for="lname">Last Name:</label><input name="lname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['lname'])){echo $_POST['lname'];}?>"/><br/>
<br/>
<label> </label><input type="submit" name="Signup" class="formButton" value="Signup" /></form>
And here is my submit.php which will validate the signup.html input
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
Now, my issue is this, in my "submit.php" file, I want to know what codes to put after the form fields validation that would enable me post the input data to another page because, I plan making it a two-page signup form. Let's say my next page is signup-2.html
how do I post the data after validation to the next page? I know how to retrieve the posted data on the next page like using Session or Echo the $_POST data but, my main issue is....how do I make the form post the data after the validation messages in my submit.php file?
use header :
header("Location: your page url?fname=$_POST['fname']&lname=$_POST['lname']");
but before this do not echo or print anything otherwise it won't redirect to that page.
you can use the data on destination page like this:
$_GET['fname']
example:
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
header('Location:download.php?fname='.$_POST['fname']."&lname=".$_POST['lname']);
view.php
<html>
<body>
<form action="submit.php" method="post">
<input type='text' id="fname" name="fname"/>
<input type='text' id="lname" name="lname"/>
<input type="submit" id="button" value="submit"/>
</form>
</body>
</html>
download.php
<?php
echo "First Name........".$_GET['fname'];
put these three file in same directory and run view.php. you will be ok.
I have a functioning "Account Notes" page that allows the user to add notes to their account for their own uses. I want to allow my admins to input notes into user accounts without logging in. I have a Dropdown on the Admin page that allows the admins to select a user. After selecting the user I want to echo the notes already in their notes section into the TextArea below and allow an update to the notes.
function getNotes(){
global $id;
$result = query("SELECT * FROM `extras` WHERE `userid`=".$id);
while ($row = mysql_fetch_assoc($result)){
return $row['notes'];
}
}
<form action="update.php" method="post">
<h4 class="widgettitle title-primary">Account Notes</h4>
<br/>
<textarea rows="20" scroll="auto" class="txt" name="notes" style="width: 80%;"/><?=getNotes();?></textarea>
<div style="text-align:center;"><button class="btn btn-primary">Update</button></div>
<input type="hidden" name="formtype" value="updatenotes" />
<input type="hidden" name="redir" value="notes.php" />
</form>
</form>
</div>
</div><!--row-fluid-->
All of the above code works correctly, so there are no issues above. My issue lies below...
The following code is what I have on my admin.php page for the user notes addition code...
<div id="addusernotes">
<form action="update.php" method="post">
<h4 class="widgettitle title-primary"><center>Add User Notes</center></h4><br/>
<?php
echo '<select id="user" name="user">';
$user = query("SELECT * FROM `users` ORDER by `user`");
if($user && mysql_num_rows($user))
{
while ($row = mysql_fetch_assoc($user))
{
echo '<option value="'.$row['id'].'">'.$row['user'].'</option>';
}
}
echo '</select>';
?><br/>
Notes:<br/>
<textarea rows="5" scroll="auto" class="txt" name="adminusernotes" style="width: 80%;"/></textarea><br/>
<input type="submit" name="adminnotes" value="Add Notes"><br/><br/>
<input type="hidden" name="formtype" value="adminusernotes" />
<input type="hidden" name="redir" value="admin.php" />
</form>
What JQuery do I need to add to the admin.php file to allow the Notes from the DB to display in the TextArea? I have been stumped on this for nearly 3 days.....Any help will be more than appreciated!!!
FYI....I have intentionally left out some of my "proprietary code for security reasons...
To alter a textarea's content with jQuery, you simply use:
$("#element-id-here").val("whatever value...");
But give your textarea an ID first. And you can change the value to a variable too.
If you want PHP to echo the textarea contents:
<textarea><?php echo "contents here"; ?></textarea>
I am learning PHP now, so pardon my silly question which I am not able to resolve.
I have created a simple web form where in I display the values entered by a user.
function submitform()
{
document.forms["myForm"].submit();
} // in head of html
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" onclick="submitForm()" />
</form>
and res.php contains:
foreach($_POST as $field => $value)
{
echo "$field = $value";
}
When I click on the submit button, I just get a blank page without any values from the form. Can anyone please let me know what am I missing?
There's no need for the javascript. This should do:
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" />
</form>
Let's start with fixing errors:
JavaScript is case-sensitive. I see that your function name is submitform and the form's onclick calls submitForm.
The javascript is not really necessary from what you've shown us, I would try this on a single php page and see if it works:
Create a test.php file for test purpose:
<?php
if($_POST){
foreach($_POST as $key=>$value){
echo "$key: $value<br />";
}
}else{
<form action="test.php" method="post">
<input type="text" value="1" name="name1" />
<input type="text" value="2" name="name2" />
<input type="submit" value="submit" name="Submit" />
</form>
}
?>
If it does work, slowly work your way into your current form setup to see what is breaking it. If it doesn't work, there's something larger at play.
There are 2 things you should do now.
Remove the JavaScript function to submit the form. It's not required (or necessary). The default behavior of a submit button is to... well... submit. You don't need to help it with JavaScript.
Enable error display by using error_reporting(E_ALL).
After you do both things, you should be able to debug and assess the problem much more easily.
Put your php code inside php tags!
<?php
foreach($_POST as $field => $value)
{
echo $field ." = ." $value.'<br />';
}
?>
If you do
<?php
print_r($_POST);
?>
what do you get?
If this still doesn't work, does your server parse php?
Create the file test.php and access it directly http://localhost/test.php or whatever your URL is
<?php
echo 'Hello';
?>
if this doesn't work..it's a whole diferent problem
You can submit an HTML form using PHP with fsubmit library.
Example:
require_once 'fsubmit.php';
$html = "<form action ='res.php' method='post'><input type='text' name='name'/></form>";
$form = new Fsubmit();
$form->url = 'http://submit_url_here.com';
$form->html = $html;
$form->params = ['name'=>'kokainom'];
$response = $form->submit();
echo $response['content'];