PHP variable setting div <p> text - php

I'm sorry if this has been asked before, I've searched around SO and nothing I've come across seems to help me here.
I have a HTML page with a form which passes values to an external PHP page.
<form method="post" action="getContent.php">
<input type="hidden" name="fromTest" id="fromTest"/>
<input type="hidden" name="untilTest" id="untilTest"/>
<input type="hidden" name="latTest" id="latTest"/>
<input type="hidden" name="longTest" id="longTest"/>
<input type="hidden" name="search1" id="search1"/>
<input type="hidden" name="search2" id="search2"/>
<input type="submit" class="sendAll" value="Gather News!">
</form>
These values are then set in the PHP to variables in SESSION
$_SESSION['post-data'] = $_POST;
$search1 = $_SESSION['post-data']["search1"];
$search2 = $_SESSION['post-data']["search2"];
$until = $_SESSION['post-data']["untilTest"];
$since = $_SESSION['post-data']["from"];
$lat = $_SESSION['post-data']["latTest"];
$long = $_SESSION['post-data']["longTest"];
This all works great and for the function it was made, everything is working perfectly.
I've got a header in my HTML of which I want the text inside of the div to be the value of $search1.
The feed1MainHeader div is completely in the HTML page and has no current links to PHP.
<div id="feed1MainHeader">
<p>test<p>
</div>
after the external php page is called and goes back to my html page, I need the header value to = my first search term.
Any idea how I can do this easily?

You can output data stored in session with
echo $_SESSION['post-data']['search1']
where you need to first search string to appear.

Related

Isset Variable Is Not Being Echoed Onto Page - PHP

I have a form that is going to submit a search query with PHP via a MySQL database. The form action attribute is set to another page, namely search.php. I want to store the search term in a variable, $searchQuery, but when I try and echo that variable out in the HTML, it doesn't show any value and I'm not getting any errors?
This is the first time I've built a search form in PHP, but I don't understand why this variable isn't being echoed?
The echo "test string"; line is being outputted as expected.
OUTPUT - search.php page
<div>
<?php
if(isset($_POST['search-submit'])) {
$searchQuery = $_POST['search-submit'];
if (isset($searchQuery)) {
echo $searchQuery;
echo "test string";
}
}
?>
</div>
FORM in the header of every page
<form action="search.php" method="POST">
<label for="search">Enter Search</label>
<input type="text" name="search" id="search">
<button type="submit" name="search-submit" id="search-submit">Search</button>
</form>
if you are demanding value from your button it should be
<button type="submit" name="search-submit" value="add value here" id="search-submit">Search</button>
but if you are demanding value from your input text it should be
$_POST['search'] "search" which is your input text name not $_POST['search-submit'] "search-submit"which is your button name

Multiple forms using PHP engine on the same page are submitted all in one go

I have multiple inquiry forms all of which call the same file used for email forwarding, so it's titled emailForwarding.php. I apparently managed to separate the forms using jQuery on the front end, but the script in emailForwarding.php is processed the same number of times as the number of the inquiry forms. I just want the php script to work for the form I submit.
I tried isolating the effect of the script using .eq() and .index() and passing an argument named $arg to only trigger form submission event for the div.vendor-wrapper containing the selected form.
single.php:
echo
'<div class="vendor-wrapper"><form method="post" action="" name="form" class="commentForm">
<textarea name="comment" placeholder="Please enter your message in the space of 300 characters and hit the Confirm button." value="" class="message" maxlength="300"></textarea>
<input name="confirm" type="button" value="Confirm">
<input class="send" name="send'.$i++.'" type="submit" value="Send">
<input type="hidden" name="position" val="">
</form></div>;
<script>
$('.confirm').click(function(){
$('.vendor-wrapper').find('.position').val('');
var index = $(this).parents('.vendor-wrapper').index()-1;
if($('.vendor-wrapper').eq(index).find('.message').val()){
$('.vendor-wrapper').eq(index).find('.confScreen').show();
$('.vendor-wrapper').eq(index).find('.position').val(index);
}
});
</script>
emailForwarding.php:
if(isset($_POST['position'])):
$arg = 'send';
$arg .= $_POST['position'];
echo "<script>console.log('PHP: ".$arg."');</script>";
if(isset($_POST[$arg])):
if(isset($_POST['comment'])):
$EmailCustomer = $_POST['email'] = $current_user->user_email;
//The rest of the script for email processing omitted.
The form is submitted the same number of times as the number of the forms on the page.
Inserting include() before tag of single.php disabled duplicate submission.
Could you provide more code? Because I was trying to reproduce the problem but could not with the provided code. As, what $_POST['position'] stands for is not clear from code.
Is the echo statement user any loop. Can you try by giving a different name to FORM?
<form method="post" action="" name="form-$i" class="commentForm">

Returning only the last value of query

This is my second code but the problem is I have 3 queries. So it only returns the last product_id when i Click update it always return product_id=3, but i want update the product_id=2
<form action="update_qty.php" method="POST">
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
<?php } ?>
</form>
Your problem is that the PHP is server side and you need something client side to read the value of the text box. You would need a page refresh to pass the text field value to the server so it could write it to the url in the anchor tag. Which is what the form submit would do, but as it would have submitted the value already the anchor tag would be pointless
To do it without a page refresh use Javascript. It would be easy to do with jQuery. You could add an event that writes whatever is entered in the text box the the anchor tags href as it is typed.
I'll do something more like this.
One form per product.In your case when you submit the form the qty value will always be the las found.
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<form action="update_qty.php" method="POST">
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
</form>
<?php } ?>
You can add more information like this
update
You can not get all values as like that because input name overwrite in every loop iteration.
For multiple values you can try in two ways like:
<?php
while($getorder = mysqli_fetch_array($order)){
$newArr[] = $getorder['price']."~".$getorder['product_id']."~". $ getorder['qty'];
} //while end
?>
<input type="hidden" name="allinputs" value="<?=$newArr?>">
Input field outside the loop.
In php explode array value with ~ and get the all values.
Other solution is that
Your input field name must be change like:
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price_<?=$getorder['product_id']?>">
<?php } ?>
Change field name in every iteration.
In current scenario either you need three different buttons or the best solution to use AJAX request .
update
On update_qty.php u can use like this
<?php echo $_GET['product_id'];?>

pass form vars through wordpress pages

Im kind of new to php, and am trying to learn how to accomplish a normaly simple task, but am running into language barrier :). Hoping you guys have some advise or a few code snippets to guide me in the right direction.
I'm using a wordpress site, and have built a page within it (using new page post), inside this page ive built a form that is calling an external js file, heres a link to this page:
( http://69.195.124.135/~fivestk2/cash-closing-cost-estimator/).
Where im having trouble is how to pass the form vars over to the next page. the next page should just be a summary of the vars, and a few extra fields to input an email by the user so that the summarized page can be emailed.
my logic is that i should be able to do this below for each var in my form
<form method="post" action="resultvars.php">
<input type="text" var="var1"/><br/>
<input type="text" var="var2"/><br/>
<input type="text" var="var3"/><br/>
<input type="text" var="var4"/><br/>
<input type="submit" var="submit"/>
</form>
accesing the posted page (resultvars.php) i thought would fill in the vars into the results page but all i get when i try is a page displaying code.
<?php $name0 = $_POST['name0'];
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
$var4 = $_POST['var4'];
echo $name0.'<br/><br/>';
echo $name1.'<br/><br/>';
echo $name2.'<br/><br/>';
echo $name3.'<br/><br/>';
echo $name3.'<br/><br/>';
?>
I just a little advise on how to code my original form above so that it will work inside these wordpress pages, i think this example i found is what i need, but im new to working with this stuff. Thanks for reading
There are better ways to create your page, but for now, sticking to your issue, you should change your HTML code to this -
<form method="POST" action="resultvars.php">
<input type="text" name="var1" value="Var 4"/><br/>
<input type="text" name="var2" value="Var 4"/><br/>
<input type="text" name="var3" value="Var 4"/><br/>
<input type="text" name="var4" value="Var 4"/><br/>
<input type="submit" name="submit" value="Submit" />
</form>
Couple that with some minor changes to your PHP and you should be able to view the Posted values -
<?php
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
$var4 = $_POST['var4'];
echo $var1.'<br/><br/>'; // You could just do - echo $_POST['var1'].'<br/><br/>';
echo $var2.'<br/><br/>';
echo $var3.'<br/><br/>';
echo $var4.'<br/><br/>';
?>

Script doesnt put information

I have written a script in php to replace in newtopic button in phpbb3
in other question, a user says me this:
In your submit.php, you can retrieve the forum ID using $_GET['f']. Now, to pass it on to application.php, you can use a hidden input field:
<form method="post" action="application.php" accept-charset="utf-8" >
$id = htmlspecialchars($_GET['f']);
<input type="hidden" name="forum_id" value="<?php echo $id; ?>"/>
When you click on the submit button, the forum ID value will also get POSTed, and you'll be able to retrieve it in application.php code using the $_POST['forum_id'].
and my code goes as here:
<form method="post" action="application.php" accept-charset="utf-8" >
$id = htmlspecialchars($_GET['f']);
<input type="hidden" name="forum_id" value="<?php echo $id; ?>"/>
.............
<fieldset class="submit-buttons">
<input value="Submit" class="button2" type="submit">
</fieldset>
This code is embedded in submit.php to use phpbb3 template.
and application.php goes as here
So I click on new topic button, and I redirect to submit.php?mode=post&f=3 and in that php there is embedded the html, the problem is that with the solution, I receive the next error:
"The forum you selected does not exist" and the addresswar goes as: viewforum.php?f=&sid=a69fb9f491d2adc11c4be3a6dac02774
so I think that forum_id (in thos case is "3" (&f=3) is not correctly sent throught php scripts
I would appreciate some help
You need to add $id = htmlspecialchars($_GET['f']); inside the <?php ?> tag,
<?php $id = htmlspecialchars($_GET['f']); ?>

Categories