Activate action onClick in PHP - php

I have the following chunk of code:
while ($row = mysql_fetch_array($result)) {
echo "<li/>".$row['trendName']."<input type='submit' name='Dispay' value='Display All Items' onclick='action='http://blah.co.uk/DisplayData.php';'/>";
echo"<br />";
}
So, this I want to do is for each element on the list to create a button. When I will click at this button I want to hit a URL.
Let's say that we have only one element on the list (Movies). By clicking on the button next to the movies, I want to move to another URL: http://blah.co.uk/DisplayData.php
and furthermore to pass to the other URL the name of the element (i.e. Movies)
Is this possible, and if so, how?

I would also recommend to use <button></button> in html 5 to create a button.
echo "<button type='submit' name='Display' onclick='javascript:location=\"http://blah.co.uk/DisplayData.php?value=$row['trendName']\"'>Display</button>";
As suggested above, a link is the proper way to do so.
<?php echo "<a href='http://blah.co.uk/DisplayData.php?value=".$row['trendName']."'>Display</a>"; ?>
You can use $_GET['value']; to retrieve your data "trendName"
However "action" is used in the <form></form> tag
<form action='http://blah.co.uk/DisplayData.php'>
<input type='hidden' name='value' value='<?php echo $row['trendName']; ?>' />
<button type='submit'>Display</button>
</form>
here you would use $_POST['value']; to retrieve your data "trendName"

onclick="this.location.href='http://blah.co.uk/DisplayData.php?value=' + $row['trendName']"
If you tell, where "someValue" should come from, I'll edit this code later.
However, I would recommend using a link instead.

Related

Input div id into text box and delete it from a text file when submit is pressed

I have been racking my brain trying to get a very particular function to work. It seems simple but I just cant figure it out. I am looking to basically get a txt file and allow someone to type in a certain id into an input box that upon the user clicking "delete" will remove only the targeted DIV id.
I have tried wrapping a PHP file around a form with no success, as well as putting the PHP directly into the submit button but nothing has worked, can anyone point me in the correct direction?
I have looked for other post here but nothing seems to be exactly what im looking for or I am wording it incorrectly. This is essentially how I want it to look:
<form action='delete.php' method='post'>
<input name='idinput' type='text' value='Enter The Certain ID Value You Want To Remove'>
<input type='submit' name='submit' value='Remove'>
</form>
Not sure about the text file bit but to remove an element from the DOM, well, you can't do this in PHP without reloading the page and apssing in some extra data na dusing some logic to not display that element...
You need to use JavaScript... or with JS with jQuery
$(function(){
$('input[name="submit"]').on('click', function() {
var val = $('input[name="idinput"]').val();
$('#or.IDorCLASSNAME_' + val).remove(); //If Id Input val is 3 this gives #or.IDorCLASSNAME_3
});
});
jQuery: https://api.jquery.com
jQuery Remove: https://api.jquery.com/remove/
jQuery DOM Removal: https://api.jquery.com/category/manipulation/dom-removal/
Tutorial: https://www.w3schools.com/jquery/jquery_dom_remove.asp
I was able to do this by placing a form that is inserted within the txt info im submitting:
<form action='delete.php' method='post'>
<input type='hidden' name='random' id='random' value='".$random."'>
<button type='submit' value='report'></button>
</form>
And also by adding this to the delete.php page:
<?php
$random = $_POST['random'];
$original = "<div id='".$random."'>";
$replacement = "<div id='".$random."' class='hidden'>";
$str = file_get_contents('submissions.txt');
$str = str_replace("$original", "$replacement",$str);
file_put_contents('submissions.txt', $str);
header('Location: index.php');
?>

Passing data between PHP webpages from a dynamically generated list

I have a PHP code which generates a dynamic list inside a form like the following, note that the list is built dynamically from database:
echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
What I need is to POST the data corresponding to the user choice when he clicks on the button for that row to another page.
If we use hyperlinks with query strings there will be no problem as I'll receive the data from the other page using a GET request and the hyperlinks would be static when showed to the user.
Also I need to obtain the user input from a textbox which is only possible with POST request.
Simply from the other page (checkList.php) I need these data for further processing:
$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];
As I have a while loop that generates the fields, I always receive the last entry form the database and not the one corresponding to the line (row) that the user chosed from the LIST.
I'm going to recommend that you clean up the names of variables so that your code can
at least tell us what it's supposed to do. It should be rare that someone looks at your code
and has a lot of trouble trying to see what you're trying to accomplish :P, ESPECIALLY when you need help with something ;]. I'm going to try some things and hope that it makes doing what you want easier to comprehend and perhaps get you your answer.
It's good to try your best to not echo large amounts of HTML unnecessarily within a script , so firstly I'm going to remove the
echos from where they are not necessary.
Secondly, I'm going to use a mysql function that returns an easier to process result.
$user = mysqli_fetch_assoc($sql)
Third, I don't know if form having a name actually does anything for the backend or frontend of php, so I'm
just going to remove some of the extra crust that you have floating around that is either invalid HTML
or just doesn't add any value to what you're trying to do as you've presented it to us.
And yes, we "note" that you're building something from the database because the code looks like it does =P.
I'm also sooo sad seeing no recommendations from the other answers in regard to coding style or anything in regard to echoing html like this :(.
<?php while($user = mysqli_fetch_assoc($sql)): ?>
<form action="checkList.php" method="post">
<input type='password' name='code' value='<?php echo $user['code'] ?>' />
<input type='hidden' name='SessionID' value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
<input type='submit' value='Take Survey' />
</form>
<?php endwhile; ?>
i not sure this is correct
echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}
and javascript
<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>
On checkList.php
$id=$_GET['id'];
echo $id;
You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields.
And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID'];
I suppose it is the easiest way to solve that.
You can try something like this:
while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
On checkList.php
<?php
$num = explode('_', $_POST['SessionID']);
$num = $num[1];
//in $num you will get the number of row where you can perform action
?>
$form = 1;
while($rows=mysqli_fetch_array($sql))
{
echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}

$_POST, image forms and mysql.How to get them working together?

I'm trying to get a website working. What I have are basically two images displayed (random, taken out of a mySQL database). What I need to do is (when the user clicks one of the images) the following:
Update the page, passing the info about the selected image (submit form);
Add one piece of data to the database (upvote the image)
I need to use $_POST to pass an array of values to the next page. So I thought:
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image.png"
value ="dat1[\"data1\",\"data2\",\"data3\"]">
<!-- If value must be a single string, I'll use hidden inputs-->
</form>
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image2.png"
value ="dat2[\"data1\",\"data2\",\"data3\"]">
</form>
Then I can upvote the selected image on the mySQL database with a little php upvote() function that updates the record. The upvoting process is done when the new page is loaded. From this, I have a couple questions:
I'm guessing the images will act as buttons, right? (They are supposed to submit the form, hence refreshing the page). If not, how can I achieve this? I'm unable to do it with a link (since I can't add the values to it). Maybe a javascript function? But I don't know how to submit the form that way either...
Once the page is reloaded, does it mean that only the data from one form has been submited, so I can retrieve the data by simply calling the PHP variable $_POST['img'] and get an array back?
EDIT: I now managed to get everything working, slightly similar to what I proposed initially. Thanks for the AJAX suggestion though, since it was what helped me solve it (looked up AJAX tutorials, found solution).
Here's my solution:
<?php
echo "<form name=\"input\" action=\"F2F.php\" method=\"POST\">";
echo "<input type=\"hidden\" name =\"table\" value=\"".$table1."\">";
echo "<input type=\"image\" name=\"nom\" src=\"".$IMG_Route1."\" value =\"".$Nom_base1."\" border=\"0\">";
echo "</form>";
?>
(where the image goes)
and then, on the header:
<?php
if ($_POST['nom']||$_POST['nom_x']){
if (!$_POST['nom']){
echo 'Could not retrieve name. $_POST[\'nom_x\'] = '.$_POST['nom_x']. mysql_error();
exit;
}
if (!$_POST['table']){
echo 'Could not retrieve table. $_POST[\'table\'] = '.$_POST['table']. mysql_error();
exit;
}
upvote($_POST['table'],$_POST['nom']);
}
?>
You can use one form and a set of radio buttons to simplify things a bit. Clicking on the label will toggle the radio button. You can use commas to separate multiple values for each checkbox, which you can then abstract later on (see below)
<form name="input" action="the_page.php" method="POST">
<ul>
<li>
<label>
<img src="whatever.jpg" />
<input type="radio" name="selectedImage" id="img1" value="12,16,19" />
</label>
</li>
<li>
<label>
<img src="whatever2.jpg" />
<input type="radio" name="selectedImage" id="img2" value="12,16,19" />
</label>
</li>
</ul>
</form>
You can detect when the radio button is selected by adding a listener for the change event, then submit the form.
$('input[name="selectedImage"]').change(function() {
$('form[name="input"]').submit();
});
To abstract the multiple values, you can then explode the form result with PHP, which will return an array of the values.
$selectedImageValues = array();
$selectedImageValues = explode(",", $_POST['selectedImage']);
From there you can pull the different values out and save the data to the database.

Create clickable URL in PHP

Trying to create a clickable URL in PHP. If you visit www.wheels4rent.net/test00.html and input location and date clicking the 'quote' will take you to the list_car5.php page. At right of this page is a 'book now' button that does not work and the text hyperlink below this. problem is when clicking the text hyperlink instead of giving me the www.thrifty.co.uk URL it gives me www.wheels4rent.net/www.thrifty.co.uk URL which obviously does not display webpage. Please assist. Also i need to also create the text hyperlink within my 'book now' button instead. below is code
$url = (string)$car->book;
echo "<tr><td width=200px><' align='right' style='padding:1px; width:100px'><b>".$car->cartype."</b><br>".$car->carsipp."<br>".$car->transmission."</td><td><b>".$car->carexample."</b></td><td><b>£".$car->price."
</b><br>Unlimited Miles</b><br><a href='$url'><input type='submit' name='Book Now' value='Book Now'><br>book now<br></a></td></tr>";
}
echo "</table>";
You need to add http:// to the beginning of the URL in the <a> tag. I can't even read the code you've posted, so that's the only answer I can give.
Instead of wrapping your in an , use the onclick attribute of the tag.
<input type='submit' name='Book Now' value='Book Now'
onclick="javascript:window.location('http://www.thrifty.co.uk')" />
Book Now
single quotes doesn't read variables.
So you could either write the whole html part in echo or you can simply close the php tag and enter the html code and then continue with your php code
for ex:
<?php
echo "<input type='submit' value='submit' onclick='<script>window.location('http://www.google.com')</script>'";
?>
or simply:
<?php
-----//php code here
----
----
?>
<input type="submit" value="submit" onclick="<script>window.location('http://www.google.com')</script>"/>
<?php
------
------
------
?>

Inserting a id value into a <a> tag?

i wanted to input a id value into a tag, where the value is hidden but i want to pass that value to another page . I have the code for input type however i have no idea how to do it on a tag.
codes for input type:
<input type='hidden' name='id' value='$id'/>
<input type='submit' value='More details' />
how to archieve the same effect on tag?
There's no way to pass information in a link (an <a> tag) besides putting it in the URL.
You can put an onclick event on the specific tag that changes the hidden input fields value. e.g.
Test
function setHidVal(idVal)
{
$(function(){ $('#myHiddenField').val(idVal); }); //You don't have to use jquery
}
So once posted, you can request the hidden field name to get value.
what is the problem? any value of a form`s input, even in a hidden input you find in
$_POST[id]
You can append in a tag like this.
Method 1:
<a href="your-page.ext?id=<? echo $id; ?>">
Method 2:
<form name="hidden_form">
<input type='hidden' name='id' value='$id'/>
</form>
<a href="your-page.ext" onclick="javascript:return submit_frm(this)" > some thing goes here</a>
<script>
function submit_frm(aelem) {
document.hidden_form.action = aelem.href;
document.hidden_form.submit();
return false;
}
</script>

Categories